On Jul 18, Groove Salad said:
>checking: env-20010712-0
>checking: env-20010712-1
>checking: env-20010712-10
>checking: env-20010712-11
>checking: env-20010712-12
[snip]
>checking: env-20010712-7
>checking: env-20010712-8
>checking: env-20010712-9
>
>How can I get them in numerical order? I tried to sort them in my script
>with the following <snip>, but, it's not what I was hoping for.
Your problem is three-fold:
1. ignore the "env-" prefix
2. sort by the major number
3. OR sort by the minor number (ignoring the "-" infix)
I suggest using a Guttman-Rosler transform. This is three-part:
1. normalize your data
2. sort it natively
3. retrieve original data
For your purposes, I suggest a normalize that turns
env-YYYYMMDD-NNN
into the number
YYYYMMDDNNN
where NNN is padded with 0's on the left to make it 3 digits.
sub normalize {
my $file = shift;
$file =~ s/^env-//;
$file =~ s/-(\d+)/sprintf "%03d", $1/;
return $file;
}
The retrieval function is just the opposite.
sub retrieve {
my $num = shift;
$num =~ s/(\d\d\d)$//;
(my $minor = $1) =~ s/^0+(?!$)//;
return "env-$num-$minor";
}
There's a small trick to my $minor variable there. The number is
something like 20010717025. It represents the file env-20010717-25. But
the number 20010717000 represents env-20010717-0. So we need to strip
LEADING 0's from the minor number, but not ALL of them. The regex
s/^0+(!$)//
means to remove one or more leading 0's, that are NOT followed by the end
of the string. This constraint means that only the first two 0's from
"000" will be removed -- if the third were removed, it would be followed
by the end of the string, and the regex would fail.
Now that we have our two functions, we are ready:
my @sorted =
map retrieve($_),
sort
map normalize($_),
@original;
That is read bottom to top -- first, we normalize the filenames, then we
sort them natively (meaning, we use Perl's standard (fast) sorting), and
then we retrieve the filenames.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]