As everybody else says,

$hash{ 'key1' }{ 'key2' }

takes the contents of $hash{ 'key1' } to be a symbolic reference, and then tries to get the value of that corresponds to 'key2' against a hash with the name = $hash{ 'key1' }

So

$hash{ 'key1' } = 'foo'; $hash{ 'key1' }{ 'key2' } = 'bar'; # (2) # (2) means find %(current pkg)::foo and do $(current pkg)::foo{ 'key2' } = 'bar';

Still a hash of hashes, but I find this rather tricky to use. You always have to be aware of the fact that $hash{ 'key1' }{ 'key2' } is being evaluated via symbols

However, $hash{ 'key1' }->{ 'key2' } would be used like this:

my %hash; $hash{ 'key1' } = {}; ## ref to hash $hash{ 'key1' }->{ 'key2' } = 'foo'; ## above equivalent to my %hash = ( key1 => { key2 => 'foo' } );

And, of course, I wrote up to here and realized that it doesn't really fit to the original problem! sigh.

I probably should have pointed to

$hash{ 'key1' } = 'foo'; $hash{ 'key1', 'key2' } = 'bar';

But I really dislike that notation. ( and the way things gets stored when you do it that way )

As for the original problem, I would have done:

my %hash = ( food => { name => 'Apple', color => 'Red' } ); my $food = $hash{ 'food' }; print "$food->{ 'color' } $food->{ 'name' }\n";

Is this explanation better ? :-)


In reply to Re: Re: Re: HoH Weirdness by lestrrat
in thread HoH Weirdness by Davious

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.