http://www.perlmonks.org?node_id=809846


in reply to Re^3: Map Vs Foreach
in thread Map Vs Foreach

I wouldn't normally do it this way, this is just to show you that it is possible:
foreach my $root (map { sqrt($_) }@data) { #do something with this square root... }

Possible, but, as you doubtless know, not desirable: This version makes two passes over the list—or, rather, one pass over the list, then one pass over its transformation—whereas

foreach my $orig ( @data ) { my $root = sqrt $orig; ... do something with square root here ... }
does the same thing in only one pass.

I do take issue, though, with the statement that map is best for 1- or 2-line transformations. I think that's a readability opinion, and these are always subjective. It also might lead some into using map for a short transformation to which it's not suited. I think a less subjective guideline, and one that might be more useful when deciding which to use, is: