my ($diagnosisString, $diagnosisNumber) = ($diagnosis =~ /^(.*)\s\[(\d*)\]/);
The neat thing about the expression used in this statement is that it evaluates to a list, which in scalar context evaluates to the number of items in the list. In the scalar context of an if conditional expression, the empty list returned by a failing match like the one above evaluates to zero, and zero is false. Thus, it's possible to immediately process the components captured from a successful match or to handle a failed match.
c:\@Work\Perl\monks>perl -wMstrict -le "for my $d ('aaaa 987', 'no joy',) { printf qq{'$d': }; if (my ($x, $y) = $d =~ m{ \A (a+) \s+ (\d+) \z }xms) { print qq{match: x is '$x'; y is '$y'}; } else { print 'no match'; } } " 'aaaa 987': match: x is 'aaaa'; y is '987' 'no joy': no match
In reply to Re: $1 returning value from previous match
by AnomalousMonk
in thread $1 returning value from previous match
by wistephens
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |