【发布时间】: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