Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Regular expressions and sort

by Aristotle (Chancellor)
on Nov 04, 2005 at 14:11 UTC ( [id://505701]=note: print w/replies, xml ) Need Help??


in reply to Regular expressions and sort

You should always, always either combine capturing patterns with conditionals or use list assignment to pull out captures.

In your case, that means I’d write the routine in either of these ways:

sub sorter ($$) { my $am = ( $_[0] =~ /$mask/ ) ? $1 : ''; my $bm = ( $_[1] =~ /$mask/ ) ? $1 : ''; $am <=> $bm || $am cmp $bm; } # or sub sorter ($$) { my ( $am ) = ( $_[0] =~ /$mask/ ); my ( $bm ) = ( $_[1] =~ /$mask/ ); $am <=> $bm || $am cmp $bm; } # note: these are not quite equivalent # after a failed match, $am and $bm will contain: # #1) an empty string # #2) undef

Never use $1 when you’re not checking for a pattern match’s success.

Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://505701]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found