Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: When would you choose foreach instead of map?

by bart (Canon)
on May 20, 2004 at 19:39 UTC ( [id://355057]=note: print w/replies, xml ) Need Help??


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

You can do the same inside a map block. foreach and map act the same in this regard. The technical term is that the loop variable, $_ for map and by default for foreach, is an "alias" to the original item. Modify the alias, and you modify the original.

When would I use map? When I want to make a copy. Otherwise, I'd let other considerations take precedence. I prefer not to use map in void context, then I use foreach — as a matter of style only, there's no real technical reason to dismiss map. I use map to calculate a function value, either in scalar (count of items) or list context.

  • 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://355057]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-26 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found