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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.