While I have no inclination to quarrel with FloydATC's code, his statement in para 1 may be misleading (I, for one, find it easier to type 3 letters rather than seven).

In fact, while some programmers use for and foreach differently, they are indistinguishable except for the spelling ...and either can be used in a C-style loop or a Perlish version with or without an explicit iterator:

C:\>perl -E "my @arr=qw(a b c d); my $a; for $a(@arr) {say $a;}" a b c d <c>C:\>perl -E "my @arr=qw(a b c d); my $a; foreach $a(@arr) {say $a;} +" a b c d

And here's brian d foy's response to a question on SO ( http://stackoverflow.com/questions/2279471/whats-the-difference-between-for-and-foreach-in-perl ) back in 2010:

"They used to be separate ideas, but now they are synonyms. Perl figures out what to do based on what's in the ( ) instead of using the keyword. Blame the people who couldn't type an extra four characters. :)
C:\>perl -e "foreach ($i=0;$i<10;$i++) { print $i }" # NB: '-e' & 'pri +nt' 0123456789

And though foy's statement doesn't offer a version or date when "used to be separate ideas" changed, nodes here in the Monastery, back at least as far as 2000, disagree -- some globally, and some only with respect to Perl 4 vs. Perl 5.

Just for completeness or merely to be excessively didactic :-), if one changes foy's (5.8?) code to my (5.18) "for" ...

C:>perl -E "for ($i=0;$i<10;$i++) { say $i }" # Irrelevant differences + '-E' & 'say' 0 1 2 3 4 5 6 7 8 9


Come, let us reason together: Spirit of the Monastery
-->

In reply to Re^2: looping in 2d array by ww
in thread looping in 2d array by Linguist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.