in reply to Re^2: Recursively walk a hash to get to an element
in thread Recursively walk a hash to get to an element
The code I posted was based on code that did use :lvalue, but I adapted it to match the OP.
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.
|
|---|