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,
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,
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
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
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
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
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.
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
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 => '
> "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
> "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
> "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
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
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
> 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
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}.
> "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
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
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,
19 matches
Mail list logo