Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: a proper sort

by Anonymous Monk
on Mar 04, 2005 at 15:07 UTC ( [id://436593]=note: print w/replies, xml ) Need Help??


in reply to a proper sort

This works assuming that the numbers are on the end, there are no leading 0's, and there are at most 5 digits (if more, it's easy to see where to adjust):
my @pretty = map {s/(?<=\D)0+//; $_} sort map {chomp; do {no warnings; sprintf "%s%05d", /(\D*)(\d* +)/}} @not_so_pretty;
The first map (that is, the bottom one in the code), normifies the strings, by padding 0's so all the numbers have equal length - if there are no trailing numbers, 0's will be used. Then we perform a regular sort, and finally, we strip off the leading 0's (which could mean all the 0's). This technique is one of the variations of the GRT.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found