Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Sort on Number Embedded in String

by friedo (Prior)
on Mar 22, 2005 at 19:14 UTC ( [id://441579]=note: print w/replies, xml ) Need Help??


in reply to Sort on Number Embedded in String

There's probably a more elegant way, but this works.

@files = sort { my ($ad) = ( $a =~ /fwlog\.(\d+)\w+/ ); my ($bd) = ( $b =~ /fwlog\.(\d+)\w+/ ); $ad <=> $bd } @files; print join "\n", @files; OUTPUT: fwlog.1Mar2005.gz fwlog.2Mar2005.gz fwlog.3Mar2005.gz fwlog.4Mar2005.gz fwlog.5Mar2005.gz fwlog.6Mar2005.gz fwlog.7Mar2005.gz fwlog.8Mar2005.gz fwlog.9Mar2005.gz fwlog.14Mar2005.gz fwlog.15Mar2005.gz fwlog.16Mar2005.gz fwlog.17Mar2005.gz fwlog.18Mar2005.gz fwlog.19Mar2005.gz fwlog.20Mar2005.gz fwlog.21Mar2005.gz

Update: I'm dumbfounded that so many have reccomended the Schwartzian for such a trivial sorting operation. Is the added complexity worth it for sorting such a small amount of data? Neither [id://JediWizard]'s nor [id://Tanktalus]'s code appear to even work correctly. (See Readmore). This is a case of over-zealous premature optimization if I've ever seen it.

Up-Update:Typos.

JediWizard's output:
fwlogfwlog.14Mar2005.gz.Mar2005 fwlogfwlog.15Mar2005.gz.Mar2005 fwlogfwlog.16Mar2005.gz.Mar2005 fwlogfwlog.17Mar2005.gz.Mar2005 fwlogfwlog.18Mar2005.gz.Mar2005 fwlogfwlog.19Mar2005.gz.Mar2005 fwlogfwlog.1Mar2005.gz.Mar2005 fwlogfwlog.20Mar2005.gz.Mar2005 fwlogfwlog.21Mar2005.gz.Mar2005 fwlogfwlog.2Mar2005.gz.Mar2005 fwlogfwlog.3Mar2005.gz.Mar2005 fwlogfwlog.4Mar2005.gz.Mar2005 fwlogfwlog.5Mar2005.gz.Mar2005 fwlogfwlog.6Mar2005.gz.Mar2005 fwlogfwlog.7Mar2005.gz.Mar2005 fwlogfwlog.8Mar2005.gz.Mar2005 fwlogfwlog.9Mar2005.gz.Mar2005

And Tanktalus's:

fwlog.9Mar2005.gz fwlog.8Mar2005.gz fwlog.7Mar2005.gz fwlog.6Mar2005.gz fwlog.5Mar2005.gz fwlog.4Mar2005.gz fwlog.3Mar2005.gz fwlog.2Mar2005.gz fwlog.21Mar2005.gz fwlog.20Mar2005.gz fwlog.1Mar2005.gz fwlog.19Mar2005.gz fwlog.18Mar2005.gz fwlog.17Mar2005.gz fwlog.16Mar2005.gz fwlog.15Mar2005.gz fwlog.14Mar2005.gz

Replies are listed 'Best First'.
Re^2: Sort on Number Embedded in String
by Dru (Hermit) on Mar 22, 2005 at 19:51 UTC
    Thanks friedo (and everyone else). I find your solution the easiest to understand. I know you didn't recommend the Schwartzian Transform for this problem, but I must read up on it since I've never even heard of it. It appears to be popular amongst the monks :-).

    This place is great, in less then 30 minutes, I received a half dozen replies.
      The ST is a neat trick, and fun to learn about. In some cases, it is a very good optimization. But you should pay attention to friedo's comments. Often times the naive sort is fast enough that the overhead of the ST is not worth it, and it will almost always be easier to read than an equivalent ST.
Re^2: Sort on Number Embedded in String
by crenz (Priest) on Mar 24, 2005 at 13:43 UTC

    I think in a case like this, it depends on what the code is meant for. If it's just to play around and to find a faster solution for yourself, using the Schwartzian Transform might be a nice idea. But if it's for a production environment, your code (IMHO) might be better suited since it is more clear. In this case, the comparison is cheap enough to warrant wasting more cycles in favour of readability.

    Remember, there will always be a programmer after you who has to read your code. And only in an ideal world is (s)he familiar with advanced Perl techniques.

Log In?
Username:
Password:

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

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

    No recent polls found