in reply to define a hash-value, using hash-ref and a list of keys
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: define a hash-value, using hash-ref and a list of keys
by Lotus1 (Vicar) on Oct 29, 2014 at 14:42 UTC | |
by roboticus (Chancellor) on Oct 29, 2014 at 15:43 UTC |