http://qs321.pair.com?node_id=1978

vroom has asked for the wisdom of the Perl Monks concerning the following question: (data structures)

How do I make a hash of hashes?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I make a hash of hashes?
by blackfrier (Initiate) on Feb 02, 2000 at 17:40 UTC
    You can declare it like:
    %hash = ( arms => { biggest => 'mine', smallest => 'yours' }, feet => { biggest => 'right', smallest => 'left' }, legs => { biggest => 'theirs', smallest => 'them', muscular => 'yes' } );
Re: How do I make a hash of hashes?
by chromatic (Archbishop) on Apr 01, 2000 at 01:13 UTC
    You can also add a hash by doing something like this:
    $hash{toes} = { biggest => 'hairy troll', smallest => 'toddler', };
    (Note that the {} make a reference to an anonymous hash. See perlman:perldsc for more gritty details.)
Re: How do I make a hash of hashes?
by Doraemon (Beadle) on May 13, 2004 at 08:10 UTC
    This might interest you:
    $other_hash{key} = ''; $hash{other} = \%other_hash; $hash{other}->{key} = '1'; # ok $tmp = $hash{other}; $$tmp{key} = '2'; # use it like this # $$hash{other}{key} = '2'; # but not like this ${$hash{other}}{key} = '3'; # this is ok $hash{other}{key} = '3'; # this too, -> unneeded after { +}
Re: How do I make a hash of hashes?
by da w00t (Sexton) on Feb 23, 2000 at 01:20 UTC
    If it was declared as a named hash of multiple dimensions, you may access it like $hash{foo}->{bar}->{beer}="free";, or like this: $hash{foo}{bar}{beer}="free"; if you prefer to drop the arrows.

    If it's an anonymous hash held in a hash reference, you access it like this:

    $r_hash->{foo}{bar}{beer}= "free";
Re: How do I make a hash of hashes?
by Doraemon (Beadle) on May 13, 2004 at 08:12 UTC
    Ahh, sorry! Not 'reference to anonymous hash', but 'reference to hash'.

    Originally posted as a Categorized Answer.