In perldoc -f map, one may read:  %hash = map { getkey($_) => $_ } @array; is just a funny way to write
%hash = (); foreach $_ (@array) { $hash{getkey($_)} = $_; }
Under this pretense, this morning I wrote something that roughly looked like ({grin} I'm not using map in a void context, by the way ):
my %res = map { $_ => do { my $re; $re .= $translations{$_} for split //; $re; } if !$res{$_}; } @words;
which I assumed, in accordance with perldoc, would roughly translate to:
my %res; foreach my $word (@rw) { $res{$word} ||= do { my $re; $re .= $translations{$_} for split //,$word; $re; } }
Obviously this isn't a literal translation, but that's unimportant right now. What confused me was that Perl threw an error (using strict), telling me that I needed to define %res in the line:   } if !$res{$_}; Thus, adding a  my %res to the beginning of my map code solved that problem. This was indicative of a larger problem, however; when benchmarking the two hash generations, it became rather obvious that Perl wasn't checking to see if that key already existed in my hash:
Benchmark: running foreach, map_it for at least 5 CPU seconds... foreach: 6 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 28 +6.87/s (n=1531) map_it: 5 wallclock secs ( 5.15 usr + 0.01 sys = 5.16 CPU) @ 13 +4.77/s (n=695)
upon investigation, it turns out that when inside map, you can not refer to the array (or hash) you are assigning to at all:
my @b = (7,5,5,6); my @a; @a = map { $_ > 5 ? $_ : $a[0] } @b;
simply throws an error, because  $a[0] will return undefined until the map call is completed. As far as I can tell this behavior is not documented and unexplainable. Perl -MO=Deparse did not provide any explanation either. I have always been under the impression that map assigns to a list, like foreach does, but this doesn't seem to be true. In truth, judging from my experiences as well as the form of a map call, map returns a list that the array or hash you have created is _then_ set to.

This, however, doesn't parallel Perl's documentation. Am I doing something stupid? Is Perl's documentation wrong? Are we both wrong? I'd love for somebody to explain this behavior with respect to map as well as any other general quirks of map's behavior if there are some.

Update: I was not commenting on this functionality of map being counterintuitive (I understand that map is returning a list that is then assigned to an array); rather, that it didn't paralel Perl's documentation (which, to me, indicated that map had some magic associated with it, that changed this more 'natural' behavior, so that @b = map { $_ } @a would translate to  push(@b,$_) for @a;.) While PodMaster seems to think that I've taken the documentation out of context, in the context I read it in--which I assume is consistent with an experienced (albeit self-taught and in no way an expert) Perl programmer would read it, this behavior was a bit confusing. I was hoping that my post would yield an explanation as to how the differences in behavior link to the opcodes associated with the these different functions.

Gyan Kapur
The Worst Node Upvoter

In reply to Map: Thou Has't Confounded Me For The Last Time! (Referring To The Array You Are Assigning To In Map) by Revelation

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.