Perl - 从复杂的哈希收集数据

时间:2015-07-27 02:19:22

标签: arrays perl hash

最近我得到了这个来自我老板的哈希信息,要求我从中收集信息。我尽我所能来完成它......不幸的是我从中得不到任何东西。这是我想要得到的......我需要UNIT标签下的Keys“3”,“9”和“10”的值,如下所示,请帮助我如何在perl中进行操作?

$VAR1 = {
      'DETAIL' => {
                  'UNIT' => [
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              },
                              {
                                '1' => 'A',
                                '2' => 'B',
                                '3' => C,
                                '4' => D,
                                '5' => 'E',
                                '6' => 'F',
                                '7' => 'Y',
                                '8' => '',
                                '9' => 'G',
                                '10' => 'H',
                                '11' => '0'
                              }
                            ],
                  'z' => 'qq'
                },
      'HEADER' => {
                    'session_id' => '123456789',
                    'trans_id' => '143580181175634',
                    'timestamp' => '143580181175634',
                    'destination' => [
                                       '24658547',
                                       '34547846'
                                     ],
                    'origin' => 'SERVER1',
                    'type' => 'UnitInfo',
                    'buffer' => 'STANDARD',
                    'origin_name' => 'TESTServer1'
                  }
    };

2 个答案:

答案 0 :(得分:3)

my @values = map { @$_{3, 9, 10} } @{ $VAR1->{DETAIL}{UNIT} };

答案 1 :(得分:1)

这是一个简单的方法。 (请注意,您的数据中包含不带引号的字符。)

my %output;
foreach my $href ( @{ $VAR1->{'DETAIL'}{'UNIT'} } ) { 
  for ( 3, 9, 10 ) { 
    push @{ $output{ $_ } }, $href->{ $_ };
  }
}

输出:

$VAR1 = {
      '3' => [
               'C',
               'C',
               'C'
             ],
      '10' => [
                'H',
                'H',
                'H'
              ],
      '9' => [
               'G',
               'G',
               'G'
             ]
    };