2013-02-14 90 views
1

如何打印所有的第二級哈希鍵給像這樣的哈希(sig_qtr,日期範圍等)的Perl中印第二級哈希鍵

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

print Dumper $xml->{entities}; 

打印輸出自卸車哈希:

$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' 
        } 
     }; 

如果將鍵放入數組中會更好。 謝謝。

回答

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

爲了把它們放進一個數組,

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

爛花通過扎伊德=) – 2013-02-14 16:36:39

+1

在5.14+,只是'鍵$ XML-> entities'(實驗性的功能) – ysth 2013-02-14 17:51:06