我对 Perl 很陌生,我正在尝试以递归方式构建散列并且无处可去。我尝试搜索动态构建散列的教程,但我能找到的只是关于散列的介绍性文章。如果您指出我正确的方向或建议一篇不错的文章/教程,我将不胜感激。
我正在尝试从具有以下形式的路径的文件中读取
one/two/three
four
five/six/seven/eight
我想建立一个像
VAR = {
one : {
two : {
three : ""
}
}
four : ""
five : {
six : {
seven : {
eight : ""
}
}
}
}
我目前使用的脚本是:
my $finalhash = {};
my @input = <>;
sub constructHash {
my ($hashrf, $line) = @_;
@elements = split(/\//, $line);
if(@elements > 1) {
$hashrf->{shift @elements} = constructHash($hashrf->{$elements[0]}, @elements );
} else {
$hashrf->{shift @elements} = "";
}
return $hashrf;
}
foreach $lines (@input) {
$finalhash = constructHash($finalhash, $lines);
}