Re: [Boston.pm] Hash of hashes question

2003-02-08 Thread Anthony R. J. Ball
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,

Re: [Boston.pm] Hash of hashes question

2003-02-08 Thread Anthony R. J. Ball
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,

Re: [Boston.pm] Hash of hashes question

2003-02-08 Thread Kee Hinckley
At 10:56 AM -0500 2/6/03, Joel Gwynn wrote: 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: Makes sense. It can hardly look to see if the subhash exists without first cr

Re: [Boston.pm] Hash of hashes question

2003-02-08 Thread Kee Hinckley
At 10:56 AM -0500 2/6/03, Joel Gwynn wrote: 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: Makes sense. It can hardly look to see if the subhash exists without first cr

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread GregLondon
Joel Gwynn wrote: > > Thanks all for the explanation, and to Uri for the article, but > especially to Greg, who has given perhaps the most cryptic and > disturbing intro to a technical explanation ever: > > > you've glimpsed the dark side of autovivification. way back when, back before perl was

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Christopher Redinger
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}, the

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Ronald J Kimball
On Thu, Feb 06, 2003 at 12:00:12PM -0500, [EMAIL PROTECTED] wrote: > what exactly do you mean when you say: > > > the exists there is not needed. assuming that > > all the values at the top level of %apples are > > hash refs, then a simple boolean test at the > > top level will work just fine.

RE: [Boston.pm] Hash of hashes question

2003-02-06 Thread Joel Gwynn
ED] > [mailto:[EMAIL PROTECTED]] On Behalf Of > [EMAIL PROTECTED] > Sent: Thursday, February 06, 2003 11:23 AM > To: Joel Gwynn > Cc: [EMAIL PROTECTED] > Subject: Re: [Boston.pm] Hash of hashes question > > > you've glimpsed the dark side of autovivificatio

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread GregLondon
Uri, Perhaps you could supply some code that would clarify your position. Here's my complete script: #!/usr/local/bin/perl -s use strict; use warnings; use Data::Dumper; my %apples = ( macintosh => {weight => '10lb', cost => '5'}, red_delicious => {weight => '15lb', cost => '

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Uri Guttman
> "DB" == Dan Boger <[EMAIL PROTECTED]> writes: DB> aren't you auto-vivifying the hash keys, are you're looking them up? by DB> checking the value of $apples{crabapple}{weight} you are automatically DB> creating those keys. perhaps 'exists' would avoid that? or you could DB> check t

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Uri Guttman
> "G" == GregLondon <[EMAIL PROTECTED]> writes: G> foreach my $t (@test) G>{ G>next unless(exists($apples{$t})); G>print "$t not found\n" unless $apples{$t}{weight} G>} as i said in another post, the exists there is not needed. assuming that all the values at the to

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Uri Guttman
> "ARJB" == Anthony R J Ball <[EMAIL PROTECTED]> writes: ARJB> It is called auto-vivification, and you can't avoid it. ARJB> If you look at a hash of a hash that doesn't exist, perl ARJB> creates the lower hash, rather than giving you an error ARJB> for trying to access a hash that

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Kee Hinckley
At 10:56 AM -0500 2/6/03, Joel Gwynn wrote: 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: Makes sense. It can hardly look to see if the subhash exists without first cr

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread GregLondon
you've glimpsed the dark side of autovivification. when you say unless($apples{$t}{weight}) you're fetching a boolean value. Perl gives you one free level of autovivification when you get/fetch a value. so it autovivifies a hash at key $t when you try and test for a sub key 'weight'. as far as I

RE: [Boston.pm] Hash of hashes question

2003-02-06 Thread Wizard
> 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: I remember reading something about this. This is due to autovivification of the the $apples{$t} value in order to look

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Evan A. Zacks
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: [...] > 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}.

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Uri Guttman
> "JG" == Joel Gwynn <[EMAIL PROTECTED]> writes: JG> my @test = qw(granny_smith crabapple); JG> foreach my $t (@test){ print "$t not found\n" unless $apples{$t}{weight} JG> } print "\n---\n"; foreach my $a (keys %apples){ JG> print "$a: weight: $apples{$a}{weig

Re: [Boston.pm] Hash of hashes question

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

Re: [Boston.pm] Hash of hashes question

2003-02-06 Thread Anthony R. J. Ball
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,