This is the expected behavior. See 'autovivification' for more information. In 
order to lookup $apples{crabapple}{weight}, $apples{crabapple} blooms into 
existence. Chapter 8 in Programming Perl 3rd edition covers this in more detail.

You would have to check for the existence of $apples{$t}, then check for the 
existence of $apples{$t}{weight} if that exists.

Quoting Joel Gwynn <[EMAIL PROTECTED]>:

> Wow.  This is driving me crazy.  I'm looking for a value in one of the
> keys in a hash, like so:
> 
> my %apples = (macintosh => {weight => '10lb', cost => '5'},
>             red_delicious => {weight => '15lb', cost => '2'},
>             fuji => {weight => '12lb', cost => '7'});
> 
> my @test = qw(granny_smith crabapple);
> 
> foreach my $t (@test){ print "$t not found\n" unless $apples{$t}{weight}
> } print "\n-----------------------\n"; foreach my $a (keys %apples){
>     print "$a: weight: $apples{$a}{weight}\n";
> }
> 
> And what I get output is this:
> 
> granny_smith not found
> crabapple not found
> 
> -----------------------
> granny_smith: weight:
> fuji: weight: 12lb
> crabapple: weight:
> red_delicious: weight: 15lb
> macintosh: weight: 10lb
> 
> Now, what's driving me crazy is that the two test values are being added
> to the hash, simply by looking for $apples{$t}{weight}.  If I simply
> look for $apples{$t}, like so:
> 
> foreach my $t (@test){ print "$t not found\n" unless $apples{$t} }
> 
> new hash members are not created.  Why should this be?
> 
> 
> _______________________________________________
> Boston-pm mailing list
> [EMAIL PROTECTED]
> http://mail.pm.org/mailman/listinfo/boston-pm
> 


_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to