in reply to Re: Finding the length of an array of arrays
in thread Finding the length of an array of arrays

It is better IMO to encourage the use of foreach rather than C style loops. (Executes faster, faster to write, easier to avoid logic errors like off by one.)
my @array = ( [ 1..3 ], [ 2..6 ] ); foreach my $outer (@array) { foreach my $element (@$outer) { print "$element\n"; } }
OTOH map is really nice...
my @array = ( [ 1..3 ], [ 2..6 ] ); print map {"$_\n"} map {@$_} @array;
:-)

Replies are listed 'Best First'.
RE: Buzzcutbuddha (C Style For and Foreach The Same): Finding the length of an array of arrays
by buzzcutbuddha (Chaplain) on Aug 18, 2000 at 16:10 UTC
    Actually, according to Merlyn, at this node for and foreach are the same and have been since the beginning of Perl. That was all cleared up in for vs foreach.

    Minor point of correction... :)
      No correction in fact. I knew they were aliases all along. But I find it clearer to indicate my thoughts by saying "for" for the classic C loop and "foreach" for iterating over a list. Particularly when I am trying to tell C programmers why they should use foreach style loops rather than for loops. ;-)
        I was correcting you because you said that foreach is faster, etc. I think it would have been better to say that foreach is Perl Style and easier to understand, and better to use for those reasons...

        just my $0.02

        Why confuse them though? I would think that telling them up front that they are the same would be easier for them in the long run. Bad enough that some perl programmers still think that they are somehow different. I never use "foreach" myself, but that's because I am exercising the first Perl Virtue. :)