访问哈希

时间:2018-05-22 14:21:44

标签: perl

如何进一步访问此哈希?使用下面的翻斗车时,

print Dumper(\%LinkData);

结果是

$VAR1 = {
          'AlternativeTo::Source' => {
                                     'ITSMConfigItem' => [
                                                         {
                                                           'Type' => 'Link',
                                                           'Content' => 'CI:0523000006',
                                                           'Link' => '/otrs5/index.pl?Action=AgentITSMConfigItemZoom;ConfigItemID=24',
                                                           'Title' => 'ConfigItem# 0523000006 (Hardware): Hardware 2'
                                                         }
                                                       ]
                                   },
          'DependsOn::Source' => {
                                 'ITSMConfigItem' => [
                                                     {
                                                       'Type' => 'Link',
                                                       'Content' => 'CI:0523000005',
                                                       'Link' => '/otrs5/index.pl?Action=AgentITSMConfigItemZoom;ConfigItemID=22',
                                                       'Title' => 'ConfigItem# 0523000005 (Hardware): Hardware 1'
                                                     }
                                                   ]
                               },
          'Includes::Target' => {
                                'ITSMConfigItem' => [
                                                    {
                                                      'Type' => 'Link',
                                                      'Content' => 'CI:0523000006',
                                                      'Link' => '/otrs5/index.pl?Action=AgentITSMConfigItemZoom;ConfigItemID=24',
                                                      'Title' => 'ConfigItem# 0523000006 (Hardware): Hardware 2'
                                                    }
                                                  ]
                              }
        };

我怎样才能将它整理成类似

的东西
AlternativeTo :: ITSMConfigItem :: ConfigItem# 0523000006 (Hardware): Hardware 2

DependsOn :: ITSMConfigItem :: ConfigItem# 0523000005 (Hardware): Hardware 1

Includes :: ITSMConfigItem :: ConfigItem# 0523000006 (Hardware): Hardware 2

目前,以下代码仅管理:

    foreach my $link_name (sort keys %LinkData) 
    {
        foreach my $subject (keys %{ $LinkData{$link_name} }) 
        {
            my @ActualLinkName = split('::', $link_name);
            print "$ActualLinkName[0] :: $subject\n";

        }
    }

/*
AlternativeTo :: ITSMConfigItem
DependsOn :: ITSMConfigItem
Includes :: ITSMConfigItem
*/

2 个答案:

答案 0 :(得分:0)

如果您希望输出列为:

AlternativeTo :: ITSMConfigItem :: ConfigItem# 0523000006 (Hardware): Hardware 2
DependsOn :: ITSMConfigItem :: ConfigItem# 0523000005 (Hardware): Hardware 1
Includes :: ITSMConfigItem :: ConfigItem# 0523000006 (Hardware): Hardware 2

您可以像这样构建循环,假设您的所有数据都像提供的转储一样:

foreach my $key (keys %$hash){
     # split and get the first element of the array
     my $name = (split('::', $key))[0];

     # Get the ITSMConfigItem from the hashmap and grab the first array 
     # element contained therein, assuming there is only 1 element in the array
     my $item = ${$hash->{$key}->{ITSMConfigItem}}[0]; 

     # One line output
     print "$name :: ITSMConfigItem :: $item->{Title}\n"
}

答案 1 :(得分:0)

假设数据结构完全如图所示:

foreach my $link_name (sort keys %LinkData) {
    print '' . (split('::', $link_name))[0] . " :: ITSMConfigItem :: " . ${$LinkData{$link_name}{'ITSMConfigItem'}}[0]->{'Title'} . "\n";
}

更通用的方法是:

foreach my $key1 (sort keys %LinkData) {
    foreach my $key2 (sort keys %{$LinkData{$key1}}) {
        foreach my $innerHash(@{$LinkData{$key1}{$key2}}) {
            print '' . (split('::', $key1))[0] . " :: $key2 :: " .$innerHash->{'Title'} . "\n";
        }
    }
}