in reply to re-using a hash reference (was: Mar)

Whenever I have to deal with deeply nested hashes, I normally save a reference to a nested hash into a separate 'temporary' variable and work with it instead. Just like here:
my $hash_b = $hash1->{'a'}->{'b'}; if ($hash_b->{c}) { $hash2->{bob} = $hash_b->{c}; }


UPDATE: to the editors, I guess someone has to fix the title of this node and making it anything other than 'Mar' ;).

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}

Replies are listed 'Best First'.
Re^2: Testing Hash Value (Idiomatic)
by tadman (Prior) on Jun 08, 2002 at 15:32 UTC
    Normally, what you might do is this:
    if (my $value = $hash1->{a}->{b}->{c}) { $hash2->{bob} = $value; }
    When doing something equivalent to deep sea diving in your nested hashes, it is often easy to keep a pointer to a value or a point in the hash. For example, if you wanted the value of either 'c' or 'd' of the 'b' branch:
    if (my $ref = $hash1->{a}->{b}) { $hash2->{bob} = $ref->{c} || $ref->{d}; }
    You would certainly want to make note of jeffa's remark about autovivification, though. If this structure were persistent, and large, then you would want to carefully test each branch before diving in.
Re: Re: Mar
by Juerd (Abbot) on Jun 08, 2002 at 15:13 UTC

    Whenever I have to deal with deeply nested hashes, I normally save a reference to a nested hash into a separate 'temporary' variable and work with it instead.

    To avoid the reference being there longer than you need, you could wrap it in a block. The array will exist as long as references to it exist.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.