Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Stupid mistakes I repeatedly make

by legato (Monk)
on Mar 28, 2005 at 18:48 UTC ( #442899=note: print w/ replies, xml ) Need Help??


in reply to Re: Stupid mistakes I repeatedly make
in thread Stupid mistakes I repeatedly make

This one, I managed to avoid. I did it once early in my Perl life, and learned the mantra before the bad habit set in:

s/something/else/ for @list;

But, how do you accomplish the same thing with map, and does it make any difference?

Anima Legato
.oO all things connect through the motion of the mind


Comment on Re^2: Stupid mistakes I repeatedly make
Download Code
Re^3: Stupid mistakes I repeatedly make
by jhourcle (Prior) on Mar 28, 2005 at 20:36 UTC
    s/something/else/ for @list;

    But, how do you accomplish the same thing with map, and does it make any difference?

    my @new_list = map { ( my $tmp = $_) =~ s/.../.../; $tmp  } @list;

    It's useful only for those times when you don't want to corrupt the original list, for whatever reason

    You could probably also do:

    @list = map { s/.../.../; $_ } @list;

    but you'd take a performance hit for building the list in memory, I would think. (I haven't benchmarked it).

    Update: Anonymous makes a good point -- faster not to assign it back to itself. (but there is still a performance hit as compared to for/foreach, so just use one of those, and only use map if you don't want to corrupt the original).

      If you just want to modify the list:
      map {s/.../.../} @list;
      and in modern Perls, that won't build a list in memory.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-28 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls