Description: 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

    There's an typo in the code; the code block in the reduce statement should be

    { \($$a->{$b}) }
    Also, when I tried the code as given (after fixing the above typo), I got the error:
    % perl rec_walk.pl Can't call method "List::Util::reduce" on unblessed reference at rec_w +alk.pl line 9.
    Here's the full code of the script that caused that error:
    use strict; my %myhash; $myhash{bedrock}{flintstone}{fred} = 3; my $ref = pointer_to_element(\%myhash, qw(bedrock flintstone fred)); sub pointer_to_element { require List::Util; return List::Util::reduce { \($$a->{$b}) } \shift, @_; }
    I couldn't figure out why the error, so I switched the code to
    use List::Util 'reduce'; sub pointer_to_element { return reduce { \($$a->{$b}) } \shift, @_; }
    and it worked great. Tres cool.

    the lowliest monk

      The reason of the error is that reduce uses a prototype not yet seen when the return statement is compiled. It's therefore interpreted as indirect object syntax (method { STATEMENTS } LIST), being do { STATEMENTS }->method(LIST) with direct syntax. The way to fix this is to call &reduce like List::Util::reduce(sub { ... }, LIST).

      ihb

      See perltoc if you don't know which perldoc to read!

Re: Recursively walk a hash to get to an element
by ikegami (Pope) on Apr 04, 2021 at 18:25 UTC

    The following is the same solution but faster since it eliminates all those slow sub calls:

    sub DiveRef { my $p = \shift; $p = \( $$p->{$_} ) for @_; $p } my %myhash; my $ref = DiveRef(\%myhash, qw( bedrock flintstone fred )); -or-- my $myhash; my $ref = DiveRef($myhash, qw( bedrock flintstone fred )); $$ref = 42; # set it to 42 $$ref++; # increment it print $$ref; # print it!
      Adding some syntactic sugar with prototype and lvalue (not sure when the + prototype was introduced UPDATE: 5.14.0 code corrected)

      Checking the ref-type would help diving arrays too. (but would probably reinvent Data::Diver )

      use v5.14; use warnings; use Test::More; sub dive(+;@) :lvalue { my $h = \ shift; $h = \($$h->{$_}) for @_; $$h; } my %hsh; my $h_ref = \%hsh; my $val = $hsh{a}{b}{c} = 3; my $val2 = 42; my @path = qw/a b c/; my $test; ;;;;;;;;;; $test = "Prototypes"; is( dive(%hsh => @path) , $val, "$test accepts \%list form"); is( dive($h_ref=> @path) , $val, "$test accepts \$ref form"); ;;;;;;;;;; $test = "assign directly to lvalue"; dive(%hsh=> @path) = $val2; is( $hsh{a}{b}{c}, $val2, $test); ;;;;;;;;;; $test = "increment in place"; dive( %hsh=> @path )++; is( $hsh{a}{b}{c}, ++$val2, $test); ;;;;;;;;;; $test = "grab scalar reference"; my $s_ref = \ dive(%hsh, @path); $$s_ref++; is( $hsh{a}{b}{c}, ++$val2, $test); ;;;;;;;;;; $test = "aliasing"; for my $alias ( dive(%hsh, @path) ) { $alias++; } is( $hsh{a}{b}{c}, ++$val2, $test); done_testing;
      OUTPUT:
      ok 1 - Prototypes accepts %list form ok 2 - Prototypes accepts $ref form ok 3 - assign directly to lvalue ok 4 - increment in place ok 5 - grab scalar reference ok 6 - aliasing 1..6

      UPDATE: cosmetic corrections

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        The code I posted was based on code that did use :lvalue, but I adapted it to match the OP.

        Original:

        I strongly recommend using an existing solution such as Data::Diver.

        use Data::Diver qw( DiveVal ); my @keys = map "level$_", 1 .. 3; my $branch = {}; DiveVal($branch, map \$_, @keys) = 'leaf'; -or- my %branch; DiveVal(\%branch, map \$_, @keys) = 'leaf';

        Obviously, it can be done without module too.

        sub DiveVal :lvalue { my $p = \shift; $p = \( $$p->{$_} ) for @_; $$p } my @keys = map "level$_", 1 .. 3; my $branch; DiveVal($branch, @keys) = 'leaf'; -or- my %branch; DiveVal(\%branch, @keys) = 'leaf';

        How my DiveVal works:

        Pre-loop: $p references $branch After loop pass 0: $p references $branch->{level1} After loop pass 1: $p references $branch->{level1}{level2} After loop pass 2: $p references $branch->{level1}{level2}{level3} Returned: $branch->{level1}{level2}{level3}

        The extra level of indirection has many benefits.

        • It removes the need to treat the last key specially.
        • It removes the need to create the hash before it's dereferenced.
        • It removes the need for the root to be a reference to a hash. Instead, any scalar can be the root, even an undefined one.
        • It makes it easy to extend DiveVal to support mixed array/hash structures.

        Being fundamentally the same solution, the explanation applies to Merlyn's code too. Just substitute $a for $p, and it's a reference to the final scalar that's returned instead of the scalar itself as an lvalue.

Re: Recursively walk a hash to get to an element
by princepawn (Parson) on Mar 31, 2005 at 00:36 UTC
    That is definitely one for Higher Order Perl, Second Edition.


    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality