Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > array evaluation in Perl question

array evaluation in Perl question
Thread Tools
Senior User
Join Date: Sep 2000
Location: Noo Yawk
Status: Offline
Reply With Quote
May 26, 2002, 11:33 AM
 
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
Reply With Quote
May 26, 2002, 04:39 PM
 
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
Reply With Quote
May 26, 2002, 04:46 PM
 
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?
     
vsurfer  (op)
Senior User
Join Date: Sep 2000
Location: Noo Yawk
Status: Offline
Reply With Quote
Jun 4, 2002, 09:16 PM
 
</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>
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 06:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2