|
|
| Come for the quick hacks, stay for the epiphanies. | |
| PerlMonks |
Re: When would you choose foreach instead of map?by dragonchild (Archbishop) |
| on May 20, 2004 at 15:48 UTC ( [id://355058]=note: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.
map is a list-level operator. foreach is a scalar-level operator. That they can often be rewritten in terms of each other is neat, but not much more.
I use map when I want to transform one list into another list. So, in your example, I would use map to transform the list. I use foreach when I want to do complex logic with each item separately. Now, I can use map for nearly every instance I use foreach, but it would require me to think more. As a Perl developer, I'm constitutionally lazy, so I'd rather not think more than I have to. So, I don't. :-) ------
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose I shouldn't have to say this, but any code, unless otherwise stated, is untested
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||