in reply to map and regexp. An newbie question
Within map, $_ is an alias to each array element (one per iteration). So if you modify $_, you modify the array element to which it is aliased for a given iteration.
The s/// operator is returning the number of times it matched. But what you seem to want is not the number of times, but the actual substitution. And you also want that to not affect the key.
my %hash = map{ my $temp = $_; $temp =~ s/a/X/g; $_, $temp } @array;
This snippet works by making a copy of the current element, then doing the substitution on that copy, and finally, returning both the original (which becomes a key) and the modified version (which becomes a value).
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: map and regexp. An newbie question
by anneli (Pilgrim) on Oct 19, 2011 at 06:56 UTC | |
by davido (Cardinal) on Oct 19, 2011 at 07:06 UTC | |
by anneli (Pilgrim) on Oct 19, 2011 at 07:11 UTC | |
by davido (Cardinal) on Oct 19, 2011 at 07:21 UTC | |
by anneli (Pilgrim) on Oct 19, 2011 at 07:50 UTC | |
| |
by nando (Acolyte) on Oct 19, 2011 at 15:35 UTC | |
|
Re^2: map and regexp. An newbie question
by nando (Acolyte) on Oct 19, 2011 at 15:31 UTC |