Not_a_Number has asked for the wisdom of the Perl Monks concerning the following question:
I would have expected the following two snippets to have the same output:
# Simple arrays: my @ary1 = ( 'a' .. 'e' ); my @ary2 = ( 0 .. 4 ); my $str = ''; $str .= $_ for @ary1, @ary2; print $str; # Array of arrays: my @AoA = ( [ 'a' .. 'e' ], [ 0 .. 4 ] ); my $str2 = ''; $str2 .= @$_ for @AoA; print $str2;
But the output from the first is:
abcde01234while that from the second is:
55Presumably, the concatenation operator is imposing scalar context in the second case (it's like if I'd done:
$str .= $_ for scalar @ary1, scalar @ary2;in the first bit).
Wherein lies the difference?
TIA
dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dereferencing and context?
by Corion (Patriarch) on Feb 08, 2004 at 18:18 UTC | |
|
Re: Dereferencing and context?
by Abigail-II (Bishop) on Feb 08, 2004 at 18:15 UTC | |
|
Re: Dereferencing and context?
by The Mad Hatter (Priest) on Feb 08, 2004 at 18:18 UTC | |
|
Re: Dereferencing and context?
by Not_a_Number (Prior) on Feb 08, 2004 at 18:47 UTC | |
by graff (Chancellor) on Feb 09, 2004 at 00:41 UTC | |
|
Re: Dereferencing and context?
by davido (Cardinal) on Feb 09, 2004 at 03:12 UTC |