in reply to Hash to Hash to Hash ....

You'll have to go over all the keys in your list. You don't need to recurse if you don't want to, though:
# traverse($hoh, @key): return value of $hoh->{$key[0]}...{$key[-1]} sub traverse { my $hoh = shift; $hoh = $hoh->{$_} for (@_); $hoh } # set($hoh,$val, @path): ... sub set { my $hoh = shift; my $val = shift; my $last = pop @_; $hoh = $hoh->{$_} for (@_); $hoh->{$last} = $val }
Note, however, that the set sub does not do auto vivification of hash keys for you. This might be regarded a feature, but probably isn't.

Replies are listed 'Best First'.
more Hash to Hash to Hash ....
by wertert (Sexton) on Aug 21, 2001 at 14:04 UTC
    Hi Me here again. Can't find the ref to 'auto vivification'. Still trying to find out more about it. Can you give me a brief run down.

    I been using the set() function which works well. The next step is to add items into the tree. To do this I am using the same idea of passing a path. The question arises when passing a new path to a empty tree. i.e
    set($ref_to_hash,1,(a c b d)) # set($hoh,$val, @path): ... by ariels sub set { my $hoh = shift; my $val = shift; my $last = pop @_; $hoh = $hoh->{$_} for (@_); $hoh->{$last} = $val }
    the set function works if the 'a c b ' already exists. If it doesn't then we simply create a hash in space somewhere ( i think ). What I need is to dynamically create nodes which do no exist. I have done this by using a temporary hash to add the new item in and then link it back into the tree.
    #create first node $hr=\%I; $hr->{'one'}=1; #create tmp node and link back in $new=\%tmp; $new->{'two'}=1; $hr->{'one'}=$new;
    Seems to work ok but I reckon you'll have a better way Thanks again wert
      just found out about auto autovivification. Seems to all tie together. The book says 'This is what happens when you try to assign through an undefined references and Perl automatically creates the reference you're trying to use.'