in reply to Re: looping in 2d array
in thread looping in 2d array
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: looping in 2d array
by FloydATC (Deacon) on Nov 02, 2014 at 13:55 UTC | |
by ww (Archbishop) on Nov 02, 2014 at 15:33 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2014 at 21:51 UTC |