Yes, that's possible. it should be something like:
use strict; use warnings; use Data::Dump qw(pp); my @a = qw(now is the time for all good men); my %h; add_val(\%h, 'FOO', @a); print pp(\%h); sub add_val { my ($hashref, $val, @keylist) = @_; die "Expected a list of keys!" unless @keylist; die "Expected a hashref!" unless "HASH" eq ref $hashref; while (@keylist > 1) { my $key = shift @keylist; $hashref->{$key} = {} if ! exists $hashref->{$key}; $hashref = $hashref->{$key}; } $hashref->{shift @keylist} = $val; }
Untested, your mileage may vary, you can keep all the pieces it might break into, join(", ",@other_disclaimers).
Update: Fixed & tested this time.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re: define a hash-value, using hash-ref and a list of keys
by roboticus
in thread define a hash-value, using hash-ref and a list of keys
by janssene
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |