NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
make use of the indices of the $tiers (Score:1)
use strict;
use warnings;
use Data::Dumper;
my $hash = {};
set_multi_level_hash($hash, [qw/ a b c d
print Dumper $hash;
sub set_multi_level_hash
{
my ( $hash, $tiers, $value ) = @_;
for (0
{
$hash->{$tiers->[$_]} ||= ( $_ == $#$tiers ? $value : {} );
$hash = $hash->{$tiers->[$_]};
}
}
Reply to This
Re:make use of the indices of the $tiers (Score:1)
$hash->{$tiers->[$_]} = {} unless exists $hash->{$tiers->[$_]};
$hash->{$tiers->[$_]} = $value if $_ == $#$tiers;
Re:make use of the indices of the $tiers (Score:2)
Oh well, too late now. It is out of my hands.
Thanks for the code
Re:make use of the indices of the $tiers (Score:2)
set_multi_level_hash($h, [qw(a b c)], 42);
set_multi_level_hash($h, [qw(a b)], 66);
Oops, we just nuked the first value and the whole "subhash".
In other words, is the level of multiness going to stay the same?
Personally, I would either create a class that guards against the multiness changing, or would make the data structure to be an array reference of hash references, keyed off in the array reference on the depth of $tiers (the latter can of course
Re:make use of the indices of the $tiers (Score:2)
I suppose you could create a class to limit operations, but then, you probably would have need ed a lot more time to deliver the code. One of the requirements was "right now". I didn't build in a lot of extra stuff to make variables not be variable.
There is no get_multi_level_hash functio