Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Perl oddities (s/// doesn't DWIM in map)

by Roy Johnson (Monsignor)
on Mar 01, 2005 at 20:18 UTC ( #435597=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl oddities (s/// doesn't DWIM in map)
in thread Perl oddities

A related oddity is that map can modify its input, in addition to generating output. That leads to the pitfall of

my @new = map { s/foo/bar/; $_ } @old; # @old is modified along with @new! # should be my @new = map { s/foo/bar/; $_ } map {$_} @old;
If map couldn't modify its input, the whole "map in a void context" debate might never have happened. for would be used for modifying input, map would be used for generating new output. Of course, it's a tradeoff, giving up some liberty for safety.

Caution: Contents may have been coded under pressure.


Comment on Re^2: Perl oddities (s/// doesn't DWIM in map)
Select or Download Code
Re^3: Perl oddities (s/// doesn't DWIM in map)
by ikegami (Pope) on Mar 01, 2005 at 22:59 UTC
    my @new = map { local $_ = $_; s/foo/bar/; $_ } @old; also avoids the problem. Probably faster than using two maps, too. Still ugly, unfortunately.
Re^3: Perl oddities (s/// doesn't DWIM in map)
by Aristotle (Chancellor) on Nov 19, 2005 at 09:02 UTC

    I write that as

    s/foo/bar/ for my @new = @old;

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://435597]
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: (12)
As of 2014-08-21 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls