"You can't make shit up and expect Perl to know what it means, retardo!"... ((in)famous MJD quote)

There must be a logical technical reason to how it should/could work. grep reads a list and checks for which ones the condition evaluates to true. That's all. That doesn't look very useful.

Now you're try this with map, it'll build a list of return values instead. Now what would the value returned from the regexp be? Why, you have capturing parens, map calls the regexp in list context, so it'll return an empty list for no match, and a list of captured values for matches. So

@result = map /\w{3}_(\w)/, @inp;
will put ('A', 'B', 'C') into the array, for your particular input.

If you foreach through that list, you can use each as a hash key, in turn. So this will do what you want:

$inp{$_} = 1 foreach map /\w{3}_(\w)/, @inp;

It looks close to what you made up, but it isn't. Not really.


In reply to Re: from array to hash with grep by bart
in thread from array to hash with grep by jeanluca

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.