Franck PORCHER wrote:
I think you're forgetting that "for" is just a synonym for "foreach". You don't need an index.But <for> is a lot easier to read and debug, IMHO .... Is there a
significant performance difference in using map instead?
My experience is that in most cases, the <for> construct is used to apply the same treatment to all the elements of an array, whence the <map> construct. In those cases, the readibility is definitely altered by the index that get in the way.
It's really not a matter of efficiency, but rather of correctness. Using map in a void context is a mistake. There is a time and place for map: when you want to do something to each element in an array and return the array on the other side. Otherwise, use for, like this:Of course, this is a quick analysis not taking into account other factors like readibility versus efficiency. In fact, regarding the efficiency of the <map> construct, I often wondered whether Perl detects <map> being ran in a void context, so as to give it an iterative interpretation, avoiding to build the output list.
some_function($_) for @array;.
Even when map is not incorrect, I usually avoid it because it has a tendency to take your code from nice, readable perl to evil h@x0r PERL in 3 characters. This is the kind of thing that Yahoo was probably worried about when the decided not to use Perl.
- Perrin