nando has asked for the wisdom of the Perl Monks concerning the following question:
I am learning to use the "map". Naturally I have been some question. For example:
my @numero = (1..100); my %lista = map{$_,$_*2} @numero; while (($num, $doble)= each(%lista)) { print "$num .... $doble\n";}
Here $ _ * 2 does not modify $ _ and the result is as expected:
33 .... 66
32 .... 64
90 .... 180
63 .... 126
21 .... 42
71 .... 142
7 .... 14
...
Instead, working with regexp $ _ variable itself is modified:
my @palabras = qw ( arriba abajo contigo compaņero cariņo superfluo vi +da); my %cosa = map {$_, $_=~s/a/X/g} @palabras; while (($uno, $dos)=each(%cosa)){print "$uno...$dos\n";}
And the result is not
arriba...XrribX
abajo...XbAjo ....
etc...
but
superfluo...
XbXjo...2
contigo...
compXņero...1
Where the %cosa key appears modified by ~ s and receives the number of substitutions made as value. It is also logical.
How do I use regexp and map if I want to have as a key each word in the array and assign it as value the result of a regexp operation on this key?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: map and regexp. An newbie question
by davido (Cardinal) on Oct 19, 2011 at 06:51 UTC | |
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 nando (Acolyte) on Oct 19, 2011 at 15:35 UTC | |
by nando (Acolyte) on Oct 19, 2011 at 15:31 UTC | |
Re: map and regexp. An newbie question
by ikegami (Patriarch) on Oct 19, 2011 at 07:42 UTC | |
by anneli (Pilgrim) on Oct 19, 2011 at 08:26 UTC | |
by nando (Acolyte) on Oct 19, 2011 at 15:44 UTC |