in reply to Assign result of map to scalar variable

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.

Replies are listed 'Best First'.
Re^2: Assign result of map to scalar variable
by AnomalousMonk (Archbishop) on Jul 19, 2013 at 02:41 UTC
    ... if the regex fails on the first value, then the scalar is undef ...

    I agree that if you want processing to stop, already, after the first match, a for-loop is the way to go.

    However, in the code you're using in your example map blocks, the value assigned to a scalar in list-context in the event of match failure would be the empty string, not undef.

    (I should mention that even though the OP is a bit unclear on this point, I assume that the  @GET_STRING array may contain any number of matching or non-matching strings in any order.)

      Yes, you are right, AnomalousMonk, it is an empty string, not undef. I made the correction.