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


In reply to Re: $1 returning value from previous match by AnomalousMonk
in thread $1 returning value from previous match by wistephens

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.