Don't use map if you need to check only the first element of the array, use shift.

Just a couple of examples under the Perl debugger:

DB<1> print map {$1 if /oo/} qw /foo bar oof rab foo/ DB<2> print map {$1 if /(oo)/} qw /foo bar oof rab foo/ oooooo DB<3> @c = map {$1 if /(oo)/} qw /foo bar oof rab foo/ DB<4> p "@c" oo oo oo DB<5> ($d) = map {$1 if /(oo)/} qw /foo bar oof rab foo/ DB<6> p $d oo DB<7> ($d) = map {$1 if /(ar)/} qw /foo bar oof rab foo/ DB<8> p $d DB<9> ($d) = map {$1 if /(ar)/} qw /foo bar oof rab foo/ DB<10> p $d

This was just to show, in the event that there was any doubt, that if the regex fails on the first value, then the scalar is undef the empty string, even if the regex in the map code block succeeds on subsequent values. In other words, everything is exactly as if you applied the regex only on the first element of the array. But if the array has millions of entries, the map will still do the work on each entries of the array, whereas we are interested only on the regex match on the first element of the array. The fact that we are keeping only one element returned by map does not lead to a lazy evaluation of map (at least not on my Perl version, i.e. 5.14), as you can see from these one-liners:

$ time perl -e '($d) = map {$1 if /(000)/} 1..999;' real 0m0.070s user 0m0.031s sys 0m0.046s $ time perl -e '($d) = map {$1 if /(000)/} 1..9999999;' real 0m18.116s user 0m17.674s sys 0m0.436s

Obviously, map is doing a lot of useless work here. Therefore, I would recommend to shift the first value of the array and apply the desired regex on it. It's much much more efficient if the array is large. It also makes more sense: map is designed to work on lists of value, no point of using it on a single value.

Then, there is also the possibility that the OP wanted to get the first successful match, not the result of the match on the first element. But, as we have seen, map is not suited for that. A map filtered with an appropriate grep would return the first successful match, but we still have the problem of having possibly a lot of unecessary work to go through all the array elements, even if the match occured early. In that case, I would recommand a foreach loop, with a last statement on successful match, to obtain what I consider to be the proper lazy behavior.

Update: corrected the "undef" to "empty string", following the comment by AnomalousMonk.


In reply to Re: Assign result of map to scalar variable by Laurent_R
in thread Assign result of map to scalar variable by ram31

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.