The question comes up frequently:
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, @_); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Recursively walk a hash to get to an element
by tlm (Prior) on Mar 31, 2005 at 02:00 UTC | |
by ihb (Deacon) on Mar 31, 2005 at 03:27 UTC | |
Re: Recursively walk a hash to get to an element
by ikegami (Patriarch) on Apr 04, 2021 at 18:25 UTC | |
by LanX (Saint) on Apr 04, 2021 at 19:20 UTC | |
by ikegami (Patriarch) on Apr 04, 2021 at 19:47 UTC | |
Re: Recursively walk a hash to get to an element
by princepawn (Parson) on Mar 31, 2005 at 00:36 UTC |
Back to
Cool Uses for Perl