Hello monks,

I have the following subroutine which I use with sort:
my $mask=/_(\d\d)_/; sub sorter ($$){ my ($am,$bm); $_[0] =~ /$mask/; $am=$1; '' =~ /()/; # A match that always succeeds $_[1] =~ /$mask/; $bm=$1; $am <=> $bm || $am cmp $bm; } @result = sort sorter @f;
The idea is to sort a list of strings based on part of the string. So I use two matches for each string passed on sorter function, but if the first match succeeds and the seconds doesn't the $1 variable is still holding the result of the fist match as if the second match never happened. If both matches are successfull everything is working as expected but if the first one matches and the second don't the second match dont affect the backreferences in $1. I found a wrokaraoud for this by placing an always succeeding empty match between the matches but I wonder is this normal behaviour?

Can anyone explain it?

Zaro

In reply to Regular expressions and sort by zaro

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.