Re: print statement creates key in hash with reference to empty array??
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 16:29:26 GMT
"rhorizon74" <rhorizon74@xxxxxxxxx> wrote in
news:1138724646.585398.120570@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
> In the code below the print val statement actually creates an key
> 'ABC' in the hash if it does not exist ( i wanted to print the value
> of the 0th element in an array , the reference to which is stored in a
> hash with key 'ABC').
That's called autovivification (sp?) You can read Uri Guttman's article:
http://www.sysarch.com/Perl/tutorials/autoviv.html
If you want to avoid it, you need to use exists, and print only when the
said element does exist.
if( exists $hash{$key} ) {
print "$hash{$key}\n";
}
> Why should a print create a new key, shouldnt it just print undefined,
> and not create any new key.
Why do you think so?
> use strict;
use warnings;
missing.
> use Data::Dumper;
> &main();
Do you know what '&' does here? If not, then don't use it.
See
perldoc perlsub
Sinan
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.
- Follow-Ups:
- References:
- print statement creates key in hash with reference to empty array??
- From: rhorizon74
- print statement creates key in hash with reference to empty array??
- Prev by Date: print statement creates key in hash with reference to empty array??
- Next by Date: Re: print statement creates key in hash with reference to empty array??
- Previous by thread: print statement creates key in hash with reference to empty array??
- Next by thread: Re: print statement creates key in hash with reference to empty array??
- Index(es):
Relevant Pages
|