wistephens has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I have a couple of matches that I'm using to extract data from a string:

What's happening here?
# Injection For Lymphangiography[75999] $orderName =~ /^(.*)\[(\d*)\]/; my $orderNameString = $1; # Lentigo maligna melanoma [172.9] $diagnosis =~ /^(.*)\s\[(\d*)\]/; my $diagnosisString = $1;
Thanks,

Bill

Replies are listed 'Best First'.
Re: $1 returning value from previous match
by tobyink (Canon) on Jul 24, 2014 at 12:51 UTC

    The string "Lentigo maligna melanoma [172.9]" contains a dot in the bracketed part, so the regexp doesn't match it. Therefore the variables $1 to $9 remain unaltered. (They are only set if there is a successful match.)

    Try something like:

    my ($diagnosisString, $diagnosisNumber) = ($diagnosis =~ /^(.*)\s\[(\d +*)\]/);

    Or alternatively:

    my $diagnosisString; $diagnosisString = $1 if $diagnosis =~ /^(.*)\s\[(\d*)\]/;

    As a general rule, don't touch the regexp-related variables unless you know that the last match succeeded.

    Update: thanks AnomalousMonk and mr_mischief for pointing out my missing $ sign in the second example. Fixed now.

      Thanks for your help, tobyink!

      That bracketed text is very variable based on the terminology used for the diagnosis. Gonna have to move to \.+

Re: $1 returning value from previous match
by AnomalousMonk (Archbishop) on Jul 25, 2014 at 00:55 UTC

    Further to tobyink's post:

    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