Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
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.


in reply to When would you choose foreach instead of map?

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. :-)

------
We are the carpenters and bricklayers of the Information Age.

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

  • Comment on Re: When would you choose foreach instead of map?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://355058]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.