Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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;

blokhead

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://314685]
help
Chatterbox?
[Discipulus]: nice Perl dreams, folks.. shower->Dumas->bed
[erix]: pere ou fils?
[Lady_Aleena]: Discipulus, good night.
[Discipulus]: pere, le vicomte de bragelonne.. wonderfull
[erix]: I'll keep that in mind (haven't read), buona notte :)
[Discipulus]: oh erix but is the fourth of cycle;read them in order
[erix]: hm, perhaps I can just read it here
Discipulus zzz...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-05-23 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?