It is called auto-vivification, and you can't avoid it.
If you look at a hash of a hash that doesn't exist, perl 
creates the lower hash, rather than giving you an error
for trying to access a hash that does not exist. Instead
do
        unless exists $apples{$t} && $apples{$t}{weight}


On Thu, Feb 06, 2003 at 10:56:06AM -0500, Joel Gwynn wrote:
> 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

-- 
 ___  __  __    __  _  _  ____    _  _  ____  ____ 
/ __)(  )(  )  /__\( \/ )( ___)  ( \( )( ___)(_  _)
\__ \ )(__)(  /(__)\\  /  )__)    )  (  )__)   )(  
(___/(______)(__)(__)\/  (____)()(_)\_)(____) (__) 
"Humankind cannot bear very much reality." -  T.S. Eliot

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

Reply via email to