in reply to Just a regex quickie

The basic problem is that your regex is capturing the leading pipe, so that to <=> you end up passing '|10' and '|5'. Not surprisingly, these values are both numerically the same as 0.

Of course, the extra leading pipe character doesn't affect the result of a cmp operation.

Changing your code as little as possible, this will do what you want - though I must say that the entire code design still makes me recoil, and I'd jump to a transform-sort-untransform structure as the other commentor is pointing out:

@lines = sort {($b =~ /(?:\|([\w\s]+)){2}/)[0] <=> ($a =~ /(?:\|([\w\s +]+)){2}/)[0] } @lines;

By the way, I discovered what was going on with judicious use of Data::Dumper:

$b = "0|aa aa|1998|aaa a|a aaa|10|aa a aa"; @a = ($b =~ /(\|[\w\s]+){2}/); use Data::Dumper; print Dumper(\@a);
Whenever there's something weird going on in my perl code, I end up sticking references to Data::Dumper in all over the place.
-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Replies are listed 'Best First'.
Re: Re: Just a regex quickie
by pseudosocrates (Sexton) on Jun 02, 2004 at 15:53 UTC
    Thanks for the courteous reply. I realise my structure may me a little ugly, and I will have a look at the alternate suggestion for when I'm doing something with a little more data.
    Data::Dumper sounds like just what I need. As you might be able to tell, my regexes often spit out weird results :-)