in reply to Assign result of map to scalar variable

UPDATE: Ugly and not recommended...
my $VAL = (grep { defined } map { /^\*\s\(?([^\)]+)\)?/ ? $1 : () } @G +ET_STRING)[0];

Consider using grep instead of map.

Replies are listed 'Best First'.
Re^2: Assign result of map to scalar variable
by AnomalousMonk (Archbishop) on Jul 18, 2013 at 20:13 UTC
    my $VAL = (grep { defined } map { /^\*\s\(?([^\)]+)\)?/ ? $1 : () } @G +ET_STRING)[0];

    The grep seems unneeded: (Update: in the quoted code) nothing from map will ever be undefined:

    >perl -wMstrict -le "my @GET_STRING = ('no', '*nine', '*(not)', '* yes', '* (yup)', '* ya)'); ;; my $first = (map { / ^ \* \s \(? ([^\)]+) \)? /x ? $1 : () } @GET_STRING)[0]; print qq{'$first'}; " 'yes'
Re^2: Assign result of map to scalar variable
by hdb (Monsignor) on Jul 18, 2013 at 20:45 UTC

    Use length instead of defined.

      Use length ...

      But why? Nothing from  map { /^\*\s\(?([^\)]+)\)?/ ? $1 : () } in toolic's OPed code will ever have zero length.