【问题标题】:Perl dereferencing individual elements in array of hash of hashPerl取消引用散列散列数组中的单个元素
【发布时间】:2013-12-30 06:53:20
【问题描述】:

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

 $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 数据库中。但是我在取消引用单个值本身时收到错误“在连接 (.) 或字符串中使用未初始化的值 $state”。代码如下:

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

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

【问题讨论】:

  • 您的代码中的$state_name 设置在哪里?它不在您提供的代码中。您的意思是在这里使用不同的变量吗?
  • 请看上面的代码。此外,$state-name 与用于创建哈希的变量相同。谢谢

标签: mysql perl hash


【解决方案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,请见谅。这只是一个猜测。)

【讨论】:

    【解决方案2】:

    了解如何“遍历”嵌套的哈希/数组。检查下面的代码。

    #!/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 ===>  
    

    在上面的代码中,我点击了散列的每一个元素并手动打印它。这样您就可以捕获散列中的任何元素,然后在以后使用它。

    【讨论】:

    • 感谢您的回复。如果年份也发生变化,我应该进行哪些更改?
    • 在您的样本数据中,只提到了 2001 年,因此我在第一个 for 循环中使用了 %{ $VAR1->{2001}。如果您一年有多个密钥,则需要通过% { $VAR1 } 拉出所有密钥。
    猜你喜欢
    • 2013-05-09
    • 2016-09-08
    • 2013-02-05
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多