Wow, lots of replies, glad I didn't post mine :D
BUT - you have to make sure you're sorting on the right thing... you won't be if you just cmp on xxMarxxxx.
You should grab the year, month and day and convert the month to the numeric equivalent and then format it as YYYYMMDD. Then <=> it
Update:
Took me a little bit to get the hang of it, but this works:
use strict;
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my %months;
my @files = qw (
fwlog.14Mar2005.gz
fwlog.15Mar2005.gz
fwlog.16Mar2005.gz
fwlog.17Mar2005.gz
fwlog.18Mar2005.gz
fwlog.19Mar2005.gz
fwlog.1Mar2005.gz
fwlog.20Dec2005.gz
fwlog.21Mar2005.gz
fwlog.2Mar2005.gz
fwlog.3Mar2005.gz
fwlog.4Mar2005.gz
fwlog.5Jan2006.gz
fwlog.6Mar2005.gz
fwlog.7Mar2005.gz
fwlog.8Mar2005.gz
fwlog.9Mar2005.gz
);
my $i = 1;
$months{$_} = sprintf( "%02d", $i++ ) for @months;
print $_, "\n" for map { $_->[0] }
sort {
$a->[1] <=> $b->[1] or
$a->[2] <=> $b->[2] or
$a->[3] <=> $b->[3]
}
map {
(split(/\./,$_,3))[1] =~ /^(\d+)([A-Za-z]+)(\d+)$/;
[$_, sprintf( "%02d", $3), $months{$2}, $1 ]
} @files;
--------------
It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|