2013-12-12 46 views
1

我有一个如下的结构,它包含散列散列散列数组。我在取消散列值时遇到错误。Perl取消引用散列散列数组中的单个元素

$VAR1 = \{ 
     '2001' => { 
        'Arunachal Pradesh' => { 
              'CHANGLANG' => [ 
                   { 
                   'wheat' => '2', 
                   'cotton' => '', 
                   'rice' => '1' 
                   } 
                  ], 
              'SUBANSIRI UPPER' => [ 
                    { 
                     'wheat' => '', 
                     'cotton' => '1', 
                     'rice' => '2' 
                    } 
                    ], 
              }, 
        'Andhra Pradesh' => { 
              'CHITTOOR' => [ 
                  { 
                  'wheat' => '34', 
                  'cotton' => '14', 
                  'rice' => '27' 
                  } 
                 ], 
              'VIZIANAGARAM' => [ 
                   { 
                   'wheat' => '2', 
                   'cotton' => '', 
                   'rice' => '8' 
                   } 
                  ], 

             } 
        } 
     }; 

我想取消引用个别值,以便我可以填充这些值到MySQL数据库。但是,我得到的错误“使用未初始化的值$状态串联(。)或字符串”,同时derefrencing个人价值本身。代码如下:

while (my ($key, $href) = each(%$stat)) { 
     my $state = $stat->{$state_name}; #where the first value is the state name & the second value is the district 
     print "$state\n"; 
} 

中华人民共和国的国家名称代码如下:

if ($line =~ m/^State:,(\w+\s\w+),/){ 
      $state_name = $1; 
      $stat->{$year}->{$state_name} = {}; 
    } 

任何其他方式通过它我可以得到单个值或者我需要将其分配到另一个哈希等。谢谢。

+0

您的代码中设置了$ state_name吗?它不在你提供的代码中。 你是否在这里使用不同的变量? –

+0

请参阅上面的代码。而且,$ state-name是用于创建散列的相同变量。谢谢 – deep

回答

1

逐步执行结构正确,你需要更多像这样的循环:

while (my ($year, $year_ref) = each(%$stat)) 
{ 
    while (my ($state, $state_ref) = each(%$year_ref)) 
    { 
     print "year = $year, state = $state\n"; 
    } 
} 

,如果你想通过整个结构进行迭代压扁它可以添加低于环的附加水平。

例如,因为你必须在你的结构五个层次,并略低于去年的水平是一个数组引用:

while (my ($year, $year_ref) = each(%$stat)) 
{ 
    while (my ($state, $state_ref) = each(%$year_ref)) 
    { 
     while (my ($city, $city_ref) = each(%$state_ref)) 
     { 
      foreach my $prod_rec (@$city_ref) 
      { 
       while (my ($prod, $qty) = each(%$prod_rec)) 
       { 
        print "year = $year, state = $state, city = $city, prod = $prod, qty = $qty\n"; 
       } 
      } 
     } 
    } 
} 

(请原谅我,如果我猜错了命名$state下的水平$city 。这只是一个猜测。)

0

了解如何“通过嵌套的散列/数组”步行。检查下面的代码。

#!/usr/bin/perl 
use warnings; 
use strict; 

my $VAR1 = { 
     '2001' => { 
        'Arunachal Pradesh' => { 
              'CHANGLANG' => [ 
                   { 
                   'wheat' => '2', 
                   'cotton' => '', 
                   'rice' => '1' 
                   } 
                  ], 
              'SUBANSIRI UPPER' => [ 
                    { 
                     'wheat' => '', 
                     'cotton' => '1', 
                     'rice' => '2' 
                    } 
                    ], 
              }, 
        'Andhra Pradesh' => { 
              'CHITTOOR' => [ 
                  { 
                  'wheat' => '34', 
                  'cotton' => '14', 
                  'rice' => '27' 
                  } 
                 ], 
              'VIZIANAGARAM' => [ 
                   { 
                   'wheat' => '2', 
                   'cotton' => '', 
                   'rice' => '8' 
                   } 
                  ], 

             } 
        } 
     }; 

foreach my $a (keys %{ $VAR1->{2001} }) 
{ 
    print $a."\n"; 
    foreach my $b (keys %{ $VAR1->{2001}->{$a} }) 
    { 
     print "\t".$b."\n"; 
     foreach my $c (@{ $VAR1->{2001}->{$a}->{$b} }) 
     { 
      #print $c."\n"; 
      foreach my $d (keys %{ $c }) 
      { 
       print "\t\t $d ===> $c->{$d} \n"; 
      } 
     } 
    } 
} 

输出:

Arunachal Pradesh 
    CHANGLANG 
     rice ===> 1 
     wheat ===> 2 
     cotton ===> 
    SUBANSIRI UPPER 
     rice ===> 2 
     wheat ===> 
     cotton ===> 1 
Andhra Pradesh 
    CHITTOOR 
     rice ===> 27 
     wheat ===> 34 
     cotton ===> 14 
    VIZIANAGARAM 
     rice ===> 8 
     wheat ===> 2 
     cotton ===> 

在上面的代码中,我打每个和散列的每一个元素和手动打印。这样你可以捕获散列中的任何元素,然后再使用它。

+0

谢谢你的回复。如果年份也发生变化,我应该做些什么改变? – deep

+0

在您的示例数据中,仅2001年提到了一年,所以我在第一个for循环中使用了'%{$ VAR1 - > {2001}'。如果您在一年中拥有多个密钥,那么您需要通过'%{$ VAR1}'拉出所有密钥。 – slayedbylucifer