in reply to Using regex in Map function
which prints:my @in = qw(aaa bbb ccc); my @out = map { s/.$/x/ } @in; print "@in\n=>\n@out\n";
Yeah, the original is modified. Basically: never modify $_ in a map, and remember that the "output" of s/// is the success count/code. To get what you want, you need to localize $_ and reuse it as the last expression evaluated in the block:aax bbx ccx => 1 1 1
which indeed shows:my @in = qw(aaa bbb ccc); my @out = map { local $_ = $_; s/.$/x/; $_ } @in; print "@in\n=>\n@out\n";
aaa bbb ccc => aax bbx ccx
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using regex in Map function
by GrandFather (Saint) on May 02, 2007 at 22:47 UTC | |
|
Re^2: Using regex in Map function
by blazar (Canon) on May 03, 2007 at 09:16 UTC | |
by narashima (Beadle) on May 03, 2007 at 18:42 UTC | |
by blazar (Canon) on May 03, 2007 at 22:43 UTC | |
|
Re^2: Using regex in Map function
by spandox (Novice) on Feb 05, 2013 at 21:14 UTC | |
by merlyn (Sage) on Feb 08, 2013 at 18:40 UTC | |
by Anonymous Monk on Jan 29, 2016 at 21:12 UTC |