Re: print statement creates key in hash with reference to empty array??



"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
.



Relevant Pages

  • Re: print statement creates key in hash with reference to empty array??
    ... rhorizon74 wrote: ... > 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 ...
    (comp.lang.perl.misc)
  • print statement creates key in hash with reference to empty array??
    ... 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 ... Prev by Date: ...
    (comp.lang.perl.misc)
  • Re: passing a hash reference to subroutine
    ... Sinan Unur wrote: ... i though the above usage was the problem, in actuality, access the hash ... ref in the subroutine was the real problem. ... the %usage. ...
    (comp.lang.perl.misc)
  • Re: Creating a list of HASHes
    ... Sinan Unur have provided interesting and useful information, ... I felt that creating a hash that included all the test ... results for each machine would be the simplest since I can use SQL ...
    (comp.lang.perl.misc)
  • Re: hash on private member variable
    ... >>>I need to use hash on a private member variable. ... > Since this is OO style perl, its memeber variable is _map. ... $self is a reference to an anonymous hash. ... Sinan Unur ...
    (comp.lang.perl.misc)