How do I start with %myhash, and a list of keys qw(bedrock flintstone fred), and get access to the value at $myhash{bedrock}{flintstone}{fred}?Well, you call pointer_to_element, like:
my $ref = pointer_to_element(\%myhash, qw(bedrock flintstone fred)); $$ref = 42; # set it to 42 $$ref++; # increment it print $$ref; # print it!
sub pointer_to_element { require List::Util; return List::Util::reduce(sub { \($$a->{$b}) }, \shift, @_); }
In reply to Recursively walk a hash to get to an element by merlyn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |