读取哈希散列中的特定值 - Perl

时间:2015-05-12 12:56:35

标签: perl hash

我有一个哈希映射哈希,如下所示。请注意,哈希映射非常庞大,其中包含PluginsStatus作为Success或Error。当一个密钥的PluginsStatus成功时,我不需要处理任何东西(我已经处理了这个场景)但是如果它的错误我需要按顺序显示 - PluginsStatusPluginspatchLogNamePluginsLogFileName_0PluginsLogFileLink_0PluginsLogFileErrors_0等。

请注意,我不知道究竟有多少个键(哈希的哈希值),即PluginsLogFileNamePluginsLogFileLinkPluginsLogFileErrors,即它是动态的。

 $VAR1 = {          'Applying Template Changes' => {
                            'PluginsLogFileErrors_2' => 'No Errors',
                            'PluginsStatus' => 'Error',
                            'PluginsLogFileName_1' => 'Applying_Template_Changes_2015-05-12_02-57-40AM.log',
                            'PluginsLogFileName_2' => 'ApplyingTemplates.log',
                            'PluginsLogFileErrors_1' => 'ERROR: FAPSDKEX-00024 : Error in undeploying  template.Cause : Unknown.Action : refer to log file for more details.',
                            'PluginspatchLogName' => '2015-05-11_08-14-28PM.log',
                            'PluginsLogFileLink_0' => '/tmp/xpath/2015-05-11_08-14-28PM.log',
                            'PluginsLogFileName_0' => '2015-05-11_08-14-28PM.log',
                            'PluginsLogFileErrors_0' => 'No Errors',
                            'PluginsLogFileLink_2' => 'configlogs/ApplyingTemplates.log',
                            'PluginsLogFileLink_1' => 'configlogs/Applying_Template_Changes_2015-05-12_02-57-40AM.log'
                           },
                'Configuring Keystore Service' => {
                            'PluginsStatus' => 'Error',
                            'PluginsLogFileName_1' => 'Configuring_Keystore_Service_2015-05-11_11-11-37PM.log',
                            'PluginsLogFileErrors_1' => 'ERROR: Failed to query taxonomy attribute AllProductFamilyAndDomains.',
                            'PluginspatchLogName' => '2015-05-11_08-14-28PM.log',
                            'PluginsLogFileLink_0' => '/tmp/xpath/2015-05-11_08-14-28PM.log',
                            'PluginsLogFileName_0' => '2015-05-11_08-14-28PM.log',
                            'PluginsLogFileErrors_0' => 'No Errors',
                            'PluginsLogFileLink_1' => 'configlogs/Configuring_Keystore_Service_2015-05-11_11-11-37PM.log'
                           },
                'Applying Main Configuration' => {
                           'PluginsStatus' => 'Error',
                           'PluginspatchLogName' => '2015-05-11_08-14-28PM.log',
                           'PluginsLogFileName_0' => 'Applying_Main_Configuration_2015-05-12_01-11-21AM.log',
                           'PluginsLogFileLink_0' => '/tmp/xpath/2015-05-11_08-14-28PM.log',
                           'PluginsLogFileErrors_0' => 'ERROR: Failed to query taxonomy attribute AllProductFamilyAndDomains.apps.ad.common.exception.ADException: Failed to query taxonomy attribute AllProductFamilyAndDomains.... 104 lines more'
                           }
        };

以下是我正在寻找的输出代码段:

Plugin name is = Applying Template Changes
PluginsStatus = Error
PluginspatchLogName = 2015-05-11_08-14-28PM.log
PluginsLogFileName_0 = 2015-05-11_08-14-28PM.log
PluginsLogFileLink_0 = /tmp/xpath/2015-05-11_08-14-28PM.log
PluginsLogFileErrors_0 = No Errors
PluginsLogFileName_1 = Applying_Template_Changes_2015-05-12_02-57-40AM.log
PluginsLogFileLink_1 = configlogs/Applying_Template_Changes_2015-05-12_02-       57-40AM.log
PluginsLogFileErrors_1 = ERROR: FAPSDKEX-00024 : Error in undeploying  template.Cause : Unknown.Action : refer to log file for more details.,
PluginsLogFileName_2 = ApplyingTemplates.log
PluginsLogFileLink_2 = configlogs/ApplyingTemplates.log
PluginsLogFileErrors_2 = No Errors`

如果有人可以帮助我,请告诉我?

2 个答案:

答案 0 :(得分:1)

您已经构建了一个不太理想的哈希值。您应该创建一个LogFile哈希元素,其中包含一个数组作为其值。之后,这个过程很简单

{
  "Applying Main Configuration" => {
    LogFile => [
      {
        Errors => "ERROR: Failed to query taxonomy attribute AllProductFamilyAndDomains.apps.ad.common.exception.ADException: Failed to query taxonomy attribute AllProductFamilyAndDomains.... 104 lines more",
        Link   => "/tmp/xpath/2015-05-11_08-14-28PM.log",
        Name   => "Applying_Main_Configuration_2015-05-12_01-11-21AM.log",
      },
    ],
    patchLogName => "2015-05-11_08-14-28PM.log",
    Status => "Error",
  },
  "Applying Template Changes" => {
    LogFile => [
      {
        Errors => "No Errors",
        Link   => "/tmp/xpath/2015-05-11_08-14-28PM.log",
        Name   => "2015-05-11_08-14-28PM.log",
      },
      {
        Errors => "ERROR: FAPSDKEX-00024 : Error in undeploying  template.Cause : Unknown.Action : refer to log file for more details.",
        Link   => "configlogs/Applying_Template_Changes_2015-05-12_02-57-40AM.log",
        Name   => "Applying_Template_Changes_2015-05-12_02-57-40AM.log",
      },
      {
        Errors => "No Errors",
        Link   => "configlogs/ApplyingTemplates.log",
        Name   => "ApplyingTemplates.log",
      },
    ],
    patchLogName => "2015-05-11_08-14-28PM.log",
    Status => "Error",
  },
  "Configuring Keystore Service" => {
    LogFile => [
      {
        Errors => "No Errors",
        Link   => "/tmp/xpath/2015-05-11_08-14-28PM.log",
        Name   => "2015-05-11_08-14-28PM.log",
      },
      {
        Errors => "ERROR: Failed to query taxonomy attribute AllProductFamilyAndDomains.",
        Link   => "configlogs/Configuring_Keystore_Service_2015-05-11_11-11-37PM.log",
        Name   => "Configuring_Keystore_Service_2015-05-11_11-11-37PM.log",
      },
    ],
    patchLogName => "2015-05-11_08-14-28PM.log",
    Status => "Error",
  },
}

答案 1 :(得分:0)

迭代哈希的keys。使用$hash{key}{inner_key}语法进入嵌套哈希。

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my %error = ( 'Applying Template Changes' => {
                  'PluginsLogFileErrors_2' => 'No Errors',
                  'PluginsStatus' => 'Error',
                  'PluginsLogFileName_1' => 'Applying_Template_Changes_2015-05-12_02-57-40AM.log',
                   # ...
                  'PluginsLogFileLink_0' => '/tmp/xpath/2015-05-11_08-14-28PM.log',
                  'PluginsLogFileErrors_0' => 'ERROR: Failed to query taxonomy attribute AllProductFamilyAndDomains.apps.ad.common.exception.ADException: Failed to query taxonomy attribute AllProductFamilyAndDomains.... 104 lines more',
              },
);
for my $step (keys %error) {
    print "Plugin name is = $step\n";
    for my $detail (sort keys %{ $error{$step} }) {
        print "$detail = $error{$step}{$detail}\n";
    }
}