Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Turning foreach into map?

by doom (Deacon)
on Apr 05, 2005 at 22:32 UTC ( [id://445120]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Turning foreach into map?
in thread Turning foreach into map?

Foreach is faster than map if you're modifying in place. If you're using foreach to push into an array, like what's happening here, then map is going to be faster. (which is why there's the recommendation not to use map if you're not doing anything with the list generated -- you might as well have just done foreach).
This is no longer the case for recent versions of perl. Now map has been optimized in void context, so if you don't do anything with the return values, it doesn't bother generating them.

In theory, the only difference between map and for/foreach now is a matter of taste/style. (I say "in theory", because I haven't benchmarked it, and sometimes the perl porters *think* they've done, isn't quite what they've done.)

Replies are listed 'Best First'.
Re^4: Turning foreach into map?
by Roy Johnson (Monsignor) on Apr 05, 2005 at 22:59 UTC
    In theory, the only difference between map and for/foreach now is a matter of taste/style.
    And the ability to have a named variable as your index.

    Caution: Contents may have been coded under pressure.
      And the context of the (last) expression in the block. For for, it's in void context, for grep it's in scalar context, for map it's in list context.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://445120]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found