Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print statement creates key in hash with reference to empty array??

Reply
Thread Tools

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

 
 
rhorizon74
Guest
Posts: n/a
 
      01-31-2006
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').

Why should a print create a new key, shouldnt it just print undefined,
and not create any new key.

Thank you
Code:

use strict;
use Data:umper;
&main();

sub main
{
my %a;
my $aref = \%a;

print "val: @{$aref->{ABC}}[0]\n";
print Dumper($aref)."\n";
}


Output:
val:
$VAR1 = {
'ABC' => []
};

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-31-2006
"rhorizon74" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> 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:umper;
> &main();


Do you know what '&' does here? If not, then don't use it.

See

perldoc perlsub


Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-31-2006

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
> 0th element in an array , the reference to which is stored in a hash
> with key 'ABC').
>
> Why should a print create a new key, shouldnt it just print undefined,
> and not create any new key.
>
> Thank you
> Code:
>
> use strict;
> use Data:umper;
> &main();
>
> sub main
> {
> my %a;
> my $aref = \%a;
>
> print "val: @{$aref->{ABC}}[0]\n";
> print Dumper($aref)."\n";
> }
>
>
> Output:
> val:
> $VAR1 = {
> 'ABC' => []
> };


lookup autovivification.

 
Reply With Quote
 
rhorizon74
Guest
Posts: n/a
 
      01-31-2006
Thanks for the help.

That explains what is happening (i had used the exists approach to work
around).
But it does seem bogus that for a simple print statement i need to
check each and every level. I havent seen too many languages do
something like this (i may be wrong).
But a print statement shouldnt really be creating any data.


Thanks for the help though.

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-31-2006
"rhorizon74" <(E-Mail Removed)> wrote in news:1138725497.627908.102130
@g44g2000cwa.googlegroups.com:

> But it does seem bogus that for a simple print statement i need to
> check each and every level. I havent seen too many languages do
> something like this


Please feel free to use one of the languages that behave in the way you
think they should.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-31-2006
A. Sinan Unur schreef:

> if( exists $hash{$key} ) {
> print "$hash{$key}\n";
> }



Hunting for alternatives:

$ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \
| perl -MO=Deparse,-x7

exists $hash{$key} and do {
print "$hash($key)\n"
};

I don't know why the do-block is needed.


$ echo 'print "$hash{$key}\n" if exists $hash{$key};' \
| perl -MO=Deparse,-x7

exists $hash{$key} and print "$hash{$key}\n";

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-31-2006
"Dr.Ruud" <(E-Mail Removed)> wrote in news:dro98o.vk.1
@news.isolution.nl:

> A. Sinan Unur schreef:
>
>> if( exists $hash{$key} ) {
>> print "$hash{$key}\n";
>> }

>
>
> Hunting for alternatives:
>
> $ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \


Check out the missing curlies in the second hash access. Not that it
changes anything.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
rhorizon74
Guest
Posts: n/a
 
      01-31-2006
Just wanted to check if i was missing anything, Didnt think anybody
woud get offended if i questioned a programming language, didnt realize
that the perl way was set in stone.

Thanks for your help anyways

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-31-2006
[ Please quote some context when you reply. ]

"rhorizon74" <(E-Mail Removed)> wrote in news:1138729475.641979.180700
@z14g2000cwz.googlegroups.com:

> Just wanted to check if i was missing anything, Didnt think anybody
> woud get offended if i questioned a programming language, didnt realize
> that the perl way was set in stone.


You did not offend anyone, at least not me.

It was obvious that you had not read either the link I posted, or the
documentation, and yet you were still opining about what should and should
not be. With that attitude, I rather not read your posts.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-31-2006
>>>>> "r" == rhorizon74 <(E-Mail Removed)> writes:

r> Thanks for the help.
r> That explains what is happening (i had used the exists approach to work
r> around).
r> But it does seem bogus that for a simple print statement i need to
r> check each and every level. I havent seen too many languages do
r> something like this (i may be wrong).
r> But a print statement shouldnt really be creating any data.

if you read my article (which sinan posted the url for) then you will
learn more about autoviv and why it is like it is. and even calling
exists will do the same thing (but that is being fixed in perl6). the
win of autovivication is much greater then the loss of rare code like
yours. if you didn't have it you would need to check each time you used a
reference in structure and either assign a new one or use the existing
one. that is massively ugly and clumsy (and i have seen newbies coding
like that all the time).

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
CGI upload creates empty file on server? Kristian Perl 1 10-21-2003 05:06 PM
hash reference as a hash key Steve Perl Misc 6 09-28-2003 10:49 AM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments