Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to substitute the elements in an array without changing the original array. - map

by imp (Priest)
on Dec 01, 2006 at 05:51 UTC ( #587129=note: print w/ replies, xml ) Need Help??


in reply to How to substitute the elements in an array without changing the original array.

See map.

my @source = (1,2,3,4); my @dest = map { $_ * 2 } @source; print "@source\n"; print "@dest\n"; #Output: #1 2 3 4 #2 4 6 8
my @source = ('bob', 'joe'); my @dest = map { $_ eq 'bob' ? ucfirst($_) : $_ } @source; print "@source\n"; print "@dest\n"; #Output: #bob joe #Bob joe


Comment on Re: How to substitute the elements in an array without changing the original array. - map
Select or Download Code
Re^2: How to substitute the elements in an array without changing the original array. - map
by perladdict (Chaplain) on Dec 01, 2006 at 08:34 UTC
    @a=("sedam","medam","kadam","sadam"); for(@b=@a) { s/dam/lam/; } print "@b\n"; print "@a";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-11-29 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls