我有一个如下所示的日志文件。
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};
}
}
}
请有人可以解释我这样做的程序。或者至少告诉我我是否要进入写入路径。谢谢你。