I\'m quite new to Perl and I\'m trying to build a hash recursively and getting nowhere. I tried searching for tutorials to dynamically build hashes, but all I could find wer
I ran your code and found a few problems:
@elements
properly.constructHash()
is a string, but on the recursive call inside, you pass an array of @elements
Try this.
use Data::Dumper;
my $finalhash = {};
my @input = split "\n", < constructHash($remainder) } ;
} else {
return { $first , "" };
}
}
foreach $lines (@input) {
my $linehash = constructHash($lines);
my $firstkey = (keys %$linehash)[0];
# print Dumper $linehash;
$finalhash->{$firstkey} = $linehash->{$firstkey};
}
print Dumper $finalhash;
It produces
$VAR1 = {
'five' => {
'six' => {
'seven' => {
'eight' => ''
}
}
},
'one' => {
'two' => {
'three' => ''
}
},
'four' => ''
};
Remember, Perl hashes aren't ordered.