in reply to descending a tree of hash references
Excuse the verbose sub names, I wanted it to be clear what each did, I created a sigle sub to being with but I didn't like the interface, I think this makes it clear what you are getting back.use strict; use warnings; my $structure = { stuff => { hierarchy => { album => { bar => 'the right one', }, }, }, }; my $path = [ 'stuff', 'hierarchy', 'album' , 'bar' ]; print get_structure_value($structure,$path) , "\n"; $structure = return_new_structure($structure,$path,'new value'); print get_structure_value($structure,$path) , "\n"; sub make_eval { my ($path) = shift; my $string = "\$hashref->"; $string .= "{$_}" for @$path; return $string; } sub get_structure_value { my ($hashref,$path) = @_; my $string = &make_eval($path); return eval($string); } sub return_new_structure { my ($hashref,$path,$set) = @_; my $string = &make_eval($path); eval("$string = '$set'"); return $hashref; }
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: Re: descending a tree of hash references
by merlyn (Sage) on Feb 19, 2002 at 23:31 UTC | |
by tachyon (Chancellor) on Feb 20, 2002 at 00:02 UTC |