SPUG: sorting hierarchical section numbers
Aaron West
tallpeak at hotmail.com
Fri Aug 11 12:58:54 PDT 2006
Sorry, apparently I didn't read your last line.
Another way, but I think yours is better (since there might possibly be some
non-outline-number strings in the input array and this would destroy them).
$ perl -le'
my @numbers = qw(
255.1
256.1
);
print for
map { join ".", (unpack q[N*], $_) }
sort map { pack q[N*], split /\./ }
@numbers;
'
255.1
256.1
-----Original Message-----
From: spug-list-bounces+tallpeak=hotmail.com at pm.org
[mailto:spug-list-bounces+tallpeak=hotmail.com at pm.org] On Behalf Of John W.
Krahn
Sent: Thursday, August 10, 2006 11:59 PM
To: SPUG
Subject: Re: SPUG: sorting hierarchical section numbers
Michael R. Wolf wrote:
> I want to sort strings that represent outline numbers.
>
> 1
> 1.1
> 1.2
> 1.3
> 1.3.1
> 1.3.2
> 1.3.2.1
> 1.2
>
> I can't find a module. Could you point me in the correct direction so
that
> I can read the fine manual? I can't seem to find the right search term;
> these are too general 'hierarchy', 'outline', 'number', 'sort'.
$ perl -le'
my @numbers = qw(
1
1.1
1.2
1.3
1.3.1
1.3.2
1.3.2.1
1.2
);
print for
map $_->[ 0 ],
sort { $a->[ 1 ] cmp $b->[ 1 ] }
map [ $_, pack q[C*], split /\./ ],
@numbers;
'
1
1.1
1.2
1.2
1.3
1.3.1
1.3.2
1.3.2.1
This should work as long as each individual number does not exceed 255, for
larger numbers use 'n*' and for really large numbers use 'N*' for the pack
format.
John
--
use Perl;
program
fulfillment
_____________________________________________________________
Seattle Perl Users Group Mailing List
POST TO: spug-list at pm.org
SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
MEETINGS: 3rd Tuesdays
WEB PAGE: http://seattleperl.org/
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.9/417 - Release Date: 8/11/2006
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.9/417 - Release Date: 8/11/2006
More information about the spug-list
mailing list