in reply to Assign result of map to scalar variable

... only one element will be the output of map.

Not so. As given in the OPed code, map will output a value for each input value it receives. If there is a match,  $1 is returned. If the match fails, the value of the last expression evaluated, the regex itself, will be the output, and this output will be  '' (the empty string, i.e., false) because the match failed. See example below in which the entire output of map is captured and displayed. The print version may have seemed to work because print will by default (depends on value of $, – see perlvar) separate printed items with an empty string, and any empty strings from map will be collapsed to nothing.

>perl -wMstrict -le "use Data::Dump; ;; my @GET_STRING = ('no', '*nine', '*(not)', '* yes', '* (yup)', '* ya)'); ;; my @ra = map { $1 if / ^ \* \s \(? ([^\)]+) \)? /x } @GET_STRING; dd \@ra; " ["", "", "", "yes", "yup", "ya"]

A variation using map could be made to work as I think you want as follows:

>perl -wMstrict -le "my @GET_STRING = ('no', '*nine', '*(not)', '* yes', '* (yup)', '* ya)'); ;; my ($first) = map { / ^ \* \s \(? ([^\)]+) \)? /x ? $1 : () } @GET_STRING; print qq{'$first'}; " 'yes'

Update: HOWEVER: A solution using map may not be the most efficient. map will always process the entire array, but you seem to want to capture only the first match; this can easily be done with a for-loop that would also allow you to terminate matching after the first match.

Update: In the second example above,  $first will always be defined except if there was no matching string in the  @GET_STRING array — but in that case, I think one could argue that  $first should be undefined!

Update: All those 'nine's should really have been 'nein' == German 'no'.     (blush)

Replies are listed 'Best First'.
Re^2: Assign result of map to scalar variable
by rjt (Curate) on Jul 18, 2013 at 20:27 UTC
    ... map will output a value for each input value it receives. If there is a match, $1 is returned. If the match fails, the value of the last expression evaluated

    Not if you return an empty list (). The usual idiom is this:

    my @map_some = map { /foo(bar)baz/ ? $1 : () } @all;

    (However, I'm apparently having a Copy/Paste-challenged day, so if you read my node within the first few minutes, it still would have had the OP's if conditional rather than the one I tested.)

      ... if you read my node within the first few minutes ...

      We may be playing update tag. I updated my original reply here (so quickly I thought no one would notice) to preface "... map will output a value..." with the qualification "As given in the OPed code, ".