 |
 |
array evaluation in Perl question
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: Noo Yawk
Status:
Offline
|
|
I am curious why the output of :
print "@E, \n"; ("44" --strings lengths concatenated )
is the same as the output of :
print "$E, \n"; ("44" --strings lengths concatenated, as expected )
in the code that follows -- I'd have thought that the result of print @E would have concatenated the strings @A and @D and produced 1 2 3 4 5 6 7 8, instead of concatenating the lengths of each array -- which is what $E does:
--
#!/usr/bin/perl -w
@A = (1,2,3,4);
@B = @A;
$C = @B;
@D = (5,6,7,8);
@E = @A . @D;
$E = @A . @D;
$F = @A . @D;
print "@A,\n";
print "@B,\n";
print "$C,\n";
print "@D,\n";
print "@E,\n";
print "$E,\n";
print "$F,\n";
print "@A @D,\n";
(output
1 2 3 4,
1 2 3 4,
4,
5 6 7 8,
44,
44,
44,
1 2 3 4 5 6 7 8
Thanks...
( I happened to be in OS9, so I did this using MacPerl)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Durham, NC
Status:
Offline
|
|
It's because the right side of
@E = @A . @D;
isn't in a list context.
When you use the concatenation operator in the above line, it evaluates @A and @D in a scalar context, each as 4, then concatenates them into "44". When you assign this scalar to the array @E, it evaluates "44" in list context, as a single-element list, so it creates a single-element array @E, where ($E[1] == "44")
This is exactly the expected behavior. Maybe you should check the section on Contexts in the Camel book. It can get pretty convoluted..
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Durham, NC
Status:
Offline
|
|
Oh, I forgot:
changing the concatenation operator to a comma (the list operator) will combine the two arrays into @E, so
print "@E,\n";
would get the output you expected. Although @E will be an 8-element array.
If you want to actually concatenate the strings of @A and @D, use
@E = "@A" . "@D";
of course that'll leave out the space between the 4 and 5, so you probably want
@E = "@A " . "@D";
Get it?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: Noo Yawk
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by slugslugslug:
<strong>Oh, I forgot:
changing the concatenation operator to a comma (the list operator) will combine the two arrays into @E, so
print "@E,\n";
would get the output you expected. Although @E will be an 8-element array.
If you want to actually concatenate the strings of @A and @D, use
@E = "@A" . "@D";
of course that'll leave out the space between the 4 and 5, so you probably want
@E = "@A " . "@D";
Get it?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Fantastic. Thanks for adding these additional situations too. The first part regarding the scalar context I will have to ruminate on a bit more (as a camel might) - but these two subsequent examples also help clarify.
Thanks again.
<small>[ 06-04-2002, 10:19 PM: Message edited by: vsurfer ]</small>
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|