Thanks Curtis. However, I am not seeing what you had mentioned in your
response. When I tested this code, it did not create a key called 'key3'.

#!/usr/bin/perl
# Test Auto-vivification

use strict;
my(%hash);
%hash   = ( 'key1'=>'val1',
                        'key2'=>'val2');
#Please note that I am trying to access a new non-existing key called 'key3'
print ("\n $hash{'key1'}\n\n $hash{'key2'}\n\n $hash{'key3'}");
while(my($key,$val) = each %hash){
        print ("$key ==> $val\n\n");
}

-----Original Message-----
From: Curtis Poe [mailto:[EMAIL PROTECTED]]
Sent: Thursday, October 18, 2001 7:23 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: What is Auto-Vivification


--- [EMAIL PROTECTED] wrote:
> Friends -- Could you briefly explain the term "auto-vivification"? I have
> seen this quite a few times in some of the responses.
> 
> A simple example to prove the point would be greatly appreciated.
> 
> Thanks,
> Rex

Rex,

Auto-vivification is trying to access a hash entry that doesn't exit.  If
that is done, the hash
entry is typically created with an 'undef' value.

my %hash = ( foo => 'bar', baz => 'qux' );

print $hash{ 'foo' }; # prints 'bar'
print $hash{ 'stuff' }; # prints nothing, but now $hash{ 'stuff' } exists
and is equal to undef

To check for a hash entry without auto-vivifying it, use 'exists'.

if ( exists $hash{ 'stuff" } ) {
    print $hash{ 'stuff' };
} else {
    print q|No %hash entry for 'stuff'.|;
}

Cheers,
Curtis "Ovid" Poe

=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to