|
Prev: Why does Net::SFTP trigger my die handler when no errors?
Next: FAQ 4.7 How do I multiply matrices?
From: rhorizon74 on 31 Jan 2006 11:24 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::Dumper; &main(); sub main { my %a; my $aref = \%a; print "val: @{$aref->{ABC}}[0]\n"; print Dumper($aref)."\n"; } Output: val: $VAR1 = { 'ABC' => [] };
From: A. Sinan Unur on 31 Jan 2006 11:29 "rhorizon74" <rhorizon74(a)yahoo.com> wrote in news:1138724646.585398.120570(a)g43g2000cwa.googlegroups.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::Dumper; > &main(); Do you know what '&' does here? If not, then don't use it. See perldoc perlsub Sinan -- A. Sinan Unur <1usa(a)llenroc.ude.invalid> (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
From: it_says_BALLS_on_your forehead on 31 Jan 2006 11:30 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::Dumper; > &main(); > > sub main > { > my %a; > my $aref = \%a; > > print "val: @{$aref->{ABC}}[0]\n"; > print Dumper($aref)."\n"; > } > > > Output: > val: > $VAR1 = { > 'ABC' => [] > }; lookup autovivification.
From: rhorizon74 on 31 Jan 2006 11:38 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.
From: A. Sinan Unur on 31 Jan 2006 11:46 "rhorizon74" <rhorizon74(a)yahoo.com> 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 <1usa(a)llenroc.ude.invalid> (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
|
Next
|
Last
Pages: 1 2 3 4 Prev: Why does Net::SFTP trigger my die handler when no errors? Next: FAQ 4.7 How do I multiply matrices? |