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?
In reply to map and regexp. An newbie question by nando
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |