Perl在嵌套哈希中打印第二级哈希键

时间:2013-02-14 16:30:32

标签: perl hash printing nested

如何打印所有我的二级哈希键(sig_qtr,日期,范围等),如下所示:

my $xml = XMLin("./${spec_file}", ForceArray => ['range', 'constant', 'question', 'date', 'sig_yr', 'sig_qtr', 'sig_mth'], KeyAttr => {});

print Dumper $xml->{entities};

打印hash的输出:

$VAR1 = {
          'sig_qtr' => [
                       {
                         'name' => 'q1',
                         'label' => 'q1'
                       },
                       {
                         'name' => 'q4',
                         'label' => 'q4'
                       }
                     ],
          'date' => [
                    {
                      'name' => 'y2_mth',
                      'label' => 'pryr_mth_curr'
                    },
                    {
                      'name' => 'y3_pod6_qtr4',
                      'label' => 'curr_qtd4'
                    }
                  ],
          'range' => [
                       {
                         'name' => 'y0_jun',
                         'end' => '20100631',
                         'start' => '20100601'
                       },
                       {
                         'name' => 'y3_oct',
                         'end' => '20131031',
                         'start' => '20131001'
                       }
                     ],
          'constant' => [
                        {
                          'spec' => '99999999 and 99999999',
                          'name' => 'none_sixmth'
                        }
                      ],
          'sig_yr' => [
                      {
                        'name' => 'y1_sig',
                        'label' => 'ye11'
                      },
                      {
                        'name' => 'y3_sig',
                        'label' => 'ytd'
                      }
                    ],
          'sig_mth' => [
                       {
                         'name' => 'y3_nov',
                         'label' => 'nov12'
                       },
                       {
                         'name' => 'y3_oct',
                         'label' => 'oct13'
                       }
                     ],
          'question' => [
                        {
                          'name' => 'ltrq',
                          'label' => 'q9'
                        },
                        {
                          'name' => 'nextprod',
                          'label' => 'q12a'
                        }
                      ],
          'backfill' => {
                        'label' => 'bf_period'
                      },
          'year' => {
                      'current' => '2013'
                    }
        };
如果将键放入数组中,

会更好。 感谢。

1 个答案:

答案 0 :(得分:4)

print "$_\n" for keys %{ $xml->entities };

将它们放入数组中,

my @keys = keys %{ $xml->entities };