zaro has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks,
I have the following subroutine which I use with sort:
Can anyone explain it?
Zaro
I have the following subroutine which I use with sort:
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?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;
Can anyone explain it?
Zaro
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regular expressions and sort
by Fletch (Bishop) on Nov 04, 2005 at 13:56 UTC | |
by zaro (Novice) on Nov 04, 2005 at 14:14 UTC | |
by Fletch (Bishop) on Nov 04, 2005 at 14:22 UTC | |
Re: Regular expressions and sort
by japhy (Canon) on Nov 04, 2005 at 14:48 UTC | |
by ioannis (Abbot) on Nov 04, 2005 at 15:16 UTC | |
by ikegami (Patriarch) on Nov 04, 2005 at 15:43 UTC | |
Re: Regular expressions and sort
by Aristotle (Chancellor) on Nov 04, 2005 at 14:11 UTC | |
Re: Regular expressions and sort
by robin (Chaplain) on Nov 04, 2005 at 14:16 UTC | |
Re: Regular expressions and sort
by Perl Mouse (Chaplain) on Nov 04, 2005 at 14:31 UTC |
Back to
Seekers of Perl Wisdom