Perl解除散列散列数组中的各个元素

时间:2013-12-12 02:07:35

标签: mysql perl hash

我有一个如下结构,其中包含哈希哈希的散列数组。我在从散列中取消引用值时遇到错误。

 $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数据库中。但我得到错误“在连接(。)或字符串中使用未初始化的值$状态”,同时降低单个值本身。代码如下:

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} = {};
    }

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

2 个答案:

答案 0 :(得分: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,请原谅我。这只是猜测。)

答案 1 :(得分: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 ===>  

在上面的代码中,我正在点击哈希的每个元素并手动打印它。这样,您可以捕获哈希中的任何元素,然后再使用它。