【问题标题】:Perl Hash of Hashes creationPerl 哈希的哈希创建
【发布时间】:2013-12-20 15:46:31
【问题描述】:

我有一个如下所示的日志文件。

Year:2001
State: A
District A  
District B
State: B
District A
District B
Year:2002
State: A
District A
District B
State: B
District A
District B
.
.
Year:2012
State: A
District A  
District B
State: B
District A
District B

我想要一个哈希值,这样:

$VAR2 = {'2001' => {
                 'state A' => { district a
                                district b
                              } 
                  'state B' => { district a
                                 district b
                              }                   
                  }
         2002' => {
                 'state A' => { district a
                                district b
                              } 
                  'state B' => { district a
                                 district b
                              }                   
                  }
};

我已经使用 3 个嵌套循环尝试了上述逻辑,如下所示:

foreach my $key (keys %hash) {
    foreach my $key2 (keys %{ $hash{$key} }) {
        foreach my $key3 (keys %{ $hash{$key}{$key2} }) {
        $value = $hash{$key}{$key2}->{$key3};
        }
    }
}

请有人向我解释一下执行此操作的程序。或者至少告诉我我是否要进入写入路径。谢谢。

【问题讨论】:

  • 看起来您已经构建了一个哈希值,现在您想要访问所有元素 - 是这样吗?
  • @FlyingFrog 我按照逻辑尝试了上述方法,但它不起作用
  • $value = $hash{$key}{$key2}->{$key3};$hash->{$key}->{$key2}->{$key3} = $value; ?也许是那里的错字?

标签: perl hash hash-of-hashes


【解决方案1】:

你可以这样做:

use strict;
use warnings;
use 5.010;
use Data::Dumper;

my $hash;
my $year;
my $state;
while(<DATA>) {
    chomp;
    if(/^Year:(\d+)/) {
        $year = $1;
        $hash->{$year} = {};
        next;
    } elsif (/^State:/) {
        $state = $_;
        $hash->{$year}{$state} = [];
        next;
    } elsif(/^District/) {
        push @{$hash->{$year}{$state}}, $_;
    }
}
say Dumper$hash;


__DATA__
Year:2001
State: A
District A  
District B
State: B
District A
District B
Year:2002
State: A
District A
District B
State: B
District A
District B
Year:2012
State: A
District A  
District B
State: B
District A
District B

输出:

$VAR1 = {
          '2002' => {
                    'State: B' => [
                                  'District A',
                                  'District B'
                                ],
                    'State: A' => [
                                  'District A',
                                  'District B'
                                ]
                  },
          '2001' => {
                    'State: B' => [
                                  'District A',
                                  'District B'
                                ],
                    'State: A' => [
                                  'District A  ',
                                  'District B'
                                ]
                  },
          '2012' => {
                    'State: B' => [
                                  'District A',
                                  'District B'
                                ],
                    'State: A' => [
                                  'District A  ',
                                  'District B'
                                ]
                  }
        };

【讨论】:

  • 为什么在上面的例子中使用 $hash 而不是 %hash?
  • @deep:没有具体原因,你也可以使用哈希。
猜你喜欢
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 2018-08-26
  • 2010-10-20
  • 2011-05-08
相关资源
最近更新 更多