in reply to When would you choose foreach instead of map?
This whole thread can be summarized in one sentence: The single non-preference-based difference is that 'map' produces the side-effect of assigning to an 'output array' whereas 'foreach' does not. Everything else is personal preference.
This thread perfectly demonstrates TMTOWTDI, and how it relates to "personal preference". In fact, what perl-folks like to call "TMTOWTDI", I call "The Law of Personal Preference". Its allegories include:
The number one determining factor to influence code-reuse is personal preference. *Every* reason to choose programming option 'foo' over programming option 'bar' is based on personal preference.In fact, to ask whether 'foo' or 'bar' is distinguishable outside the realm of personal preference is nearly *certain* to give you emotionally-influenced answers that are based *substantially* on personal preference.
Proof: Rephrase the original question according to the following template:
Is it possible to generate the result 'X' in such a way that I can automatically deduce the method that was used to generate that result (without looking at the source code) by evaluating one or more quantifiable measures of fitness?... in other words, make up a little 'Turing test' ...
Find a person who can look at the output of a script and determine whether the output came from 'map' or from 'foreach'. They can evaluate everything except the source code itself. (eg, quantifiable measures of fitness, such as how long it takes the script to run, or complile, or whether the output meets a specification, etc. ). Hold all other things equal.If your 'quantifiable measure of fitness' is *specifically* how many lines of code it takes to do something (which is a valid measure in itself). Then just look at the different ways of doing it and pick which one seems best for you. Absent an indication that one option is inherently less secure, more buggy, less performant, more likely to be deprecated, (etc etc ad nasuem) than the others, there is no reason why you should not use one over the other (other than ... you guessed it ... personal preference).
### TMTOWTDI : ### - SnippetID: tsid="20040520_1649_06239" ### description: multiple ways to loop and munge an array ### details: | ### The following snippet shows a handful of ### alternate coding styles for looping and ### munging an array. use strict; use warnings; my (@array, @outcopy) = (); @array = qw(1 2 3 4 5); ### NOTE: we *dont* care about making an 'output copy' of the array $_ *= 2 foreach @array; print "@array"; print "\n............. \n"; $_ *= 2 for @array; print "@array"; print "\n--------------\n"; map {$_ *= 2 } @array; print "@array"; print "\n............. \n"; for (@array) {$_ *= 2 }; print "@array"; print "\n--------------\n"; foreach (@array) {$_ *= 2 }; print "@array"; print "\n............. \n"; ### NOTE: here we DO care about making an 'output copy' ### notice that both produce same effect, but foreach ### requires additional code because it ### does not produce the side-effect @outcopy = map {$_ *= 2 } @array; print "@outcopy"; print "\n--------------\n"; @outcopy = (); ###<-- additional line foreach (@array) {push @outcopy, $_ *= 2 }; print "@outcopy"; print "\n............. \n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: When would you choose foreach instead of map?
by BrowserUk (Patriarch) on May 21, 2004 at 02:13 UTC | |
by eric256 (Parson) on May 21, 2004 at 04:17 UTC | |
by Abigail-II (Bishop) on May 21, 2004 at 11:19 UTC | |
by eric256 (Parson) on May 21, 2004 at 15:02 UTC | |
by Anonymous Monk on May 21, 2004 at 05:39 UTC | |
by BrowserUk (Patriarch) on May 21, 2004 at 10:28 UTC | |
by dimar (Curate) on May 21, 2004 at 16:49 UTC | |
by BrowserUk (Patriarch) on May 21, 2004 at 16:59 UTC |