in reply to HoH Weirdness

Somehow I think you're not using strict.

On my machine $hash{ 'key1' }{ 'key2' } gives me a compilation time error with strict

Perhaps you meant $hash{ 'key1' }->{ 'key2' } ?

Either way, do this to verify if your hashes are working the way you want them to...

use Data::Dumper; print Dumper( \%hash );

This is quick and dirty, but at least you get to do a sanity check as to what's in the hash...

----

update: added explanation in a separate node upon request :)

update: a few people have gotten me. for those of you reading my posts in this thread, please make sure to read these two posts, cause I screwed up. :(

Replies are listed 'Best First'.
Re: Re: HoH Weirdness
by John M. Dlugosz (Monsignor) on Jun 09, 2001 at 00:19 UTC
    Perhaps you meant $hash{ 'key1' }->{ 'key2' } ?

    Care to continue that thought with an explaination of how

    $hash{ 'key1' }->{ 'key2' }
    is different in meaning from
    $hash{ 'key1' }{ 'key2' }
    ?

      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 ? :-)

        I think a review of perlref is in order. $hash{ 'key1' }{ 'key2' } and $hash{ 'key1' }->{ 'key2' } are exactly equivalent.

        ->, the dereferencing operator, is optional between subscripts.

        No, I still wonder. Perlref says "The arrow is optional between brackets subscripts" so I don't see how explicitly putting the arrow in makes the compiler do things differently -- it still needs the first subscript to return a reference, and it doesn't matter whether it's symbolic or not.