Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: $_ and list flattening with map()

by blokhead (Monsignor)
on Dec 14, 2003 at 21:23 UTC ( #314685=note: print w/ replies, xml ) Need Help??

in reply to $_ and list flattening with map()

$_++ for map @$_, @{$a};
The difference is that you do the increment outside of the scope in which map aliases $_. This foreach is iterating over the return value of map, which is a list of copies (not aliases back into @$a). foreach now aliases $_ to this anonymous list of copies, so your modifications to $_ are lost.

If you use map as a control structure (making it more of a direct translation of the first example), and do the increment inside map's block, you'll get what you expected originally:

map { map { $_++ } @$_ } @$a;


Comment on Re: $_ and list flattening with map()
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2015-11-26 21:51 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (707 votes), past polls