I suppose you wrote that before I edited my message?

Honestly I can't remember seeing the map in void context for a while, except when I mention that people keep bringing up the subject myself (but I did see it often in my earlier readings on perlmonks), make of that what you will.

One reason for the discussion is that map is supposed to build a new list, and not using that output is a waste of resources. But even if map wasn't smart about its context, it's perl, you're supposed to ignore performance issues most of the time and focus on getting things done. The other issue is when you use the aliasing mechanism, ie the fact that modifying $_ will modify the input. Since ; map { $_ += 2 } @numbers; and ; $_ += 2 for @numbers; do exactly the same thing, it's clearer if you use the version that's not supposed to create a separate output. Best reason to use the second version is that you won't get told that there's a better way to do it when you post it on perlmonks :D

NB: even without using the aliasing mechanism: ; map { $h{$_}++} @keys; may be better written as ; $h{$_}++ for @keys;, because map does something from the input, while for does something with the input. Nitpicking at its finest :)


In reply to Re^3: I want to operate on some values in a hash by Eily
in thread I want to operate on some values in a hash by misterperl

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.