For me it's more a matter of clarity/readability for someone looking over my code, particularly with nested maps like this (do you really find map { map { ... } @bar } @foo easier to mentally parse than foreach (@foo) { foreach (@bar) { ... } }?). But, you're right...it's mostly a stylistic thing and this is a classic example of TMTOWTDI.
Update: I tried a little test and foreach was actually significantly faster (for this particular test, that is):
#!perl -w use strict; use Benchmark; Benchmark::cmpthese(-5, { 'foreach' => sub { my $sum=0; foreach (1..100) { foreach (1..100) +{ $sum++ }}}, 'map' => sub {my $sum=0; map { map { $sum++ } (1..100) } (1..100) +}, });
------------------------------ Rate map foreach map 345/s -- -43% foreach 603/s 75% --
What gives?
-b
In reply to Re^3: how to avoid nested looping?
by bgreenlee
in thread how to avoid nested looping?
by lamp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |