I'd like to have a possibility to use map or grep with value couples instead of just one single $_. It should make it possible to loop through a hash while having easy access to both key and value, or through an array but taking two elements at a time. For example:

%h = ( key1 => 123, key2 => 'acb', key3 => 5, ); # create second hash with only numeric values %h_numbers = grep2 { looks_like_number($_[1]) } %h; # prefix all values with "p" %h_prefixed = map2 { ($_[0], "p".$_[1]) } %h; @a = ( p1 => 111, p2 => 222, p3 => 333 ); # transforms array of ordered couples to array of single element hashe +s @a_hashed = map2 { +{ $_[0] => $_[1] } } @a;

What are the clever options to achieve this? Thank you!


In reply to map2 {} grep2 {} ... by jozef

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.