Dan Jacobson said:
my @a=(1,2,3); my @b=(4,5,6);
for((((@a))),(((@b)))){ #What do I have to do to this line to make
Yikes! You need a quick trip to 'perldoc perldata'. Do not pass go,
do not collect $200. I'm not trying to make you feel bad here, but
this is very basic stuff. If you didn't read this in perldata, you
probably need to sit down and read that manpage, and probably at least
skim perltoc as well.
A short excerpt from perldata:
LISTs do automatic interpolation of sublists. That is, when a
LIST is evaluated, each element of the list is evaluated in
list context, and the resulting list value is interpolated into
LIST just as if each individual element were a member of LIST.
So, what you're iterating over is the list (((@a))),(((@b))), which is
equivalent to (@a),(@b), which is equivalent to @a,@b, which is in
turn reduced to a list containing all the elements of @a followed by
all the elements of @b.
As a stylistic aside, if I ever find myself using $_ explicitly in a
loop, that's usually a sign to me that I need to be using an explicit
name for the variable. In your case, I might write it:
for my $element (@a,@b) {
....
}
And then use $element where you have $_. This won't work in EVERY
case, but it does work 9 times out of 10.
print "new array\n"; #this line get printed only twice, not six times?
for($_){
print "element $_\n"}}
Also, this is kinda ugly from a personal POV: it doesn't match any
standard indenting style I'm familiar with, and it makes it harder to
find the close bracket for the outer for by hiding it at the end of
the inner for. And even that one might be missed on a cursory
reading.
Anyway, to answer your question, the "right" answer depends on why you
want to know. If you want to count how many arrays you put in the top
of the for loop, well, that's a bit odd, since you're specifying them
explicitly. You probably want to iterate over references to the
arrays, except then you lose the name of the array when you do so (not
sure if that would be a problem or not).
That might look like:
for my $array_ref (\@a, \@b) {
print "new array, ref value: [$ref]";
}
read perlretoot for more info on references in Perl. Also perlref.
-=Eric