SPUG: sorting hierarchical section numbers

Aaron West tallpeak at hotmail.com
Fri Aug 11 14:03:12 PDT 2006


If they can include letters, perhaps sprintf is better (won't work with
Roman numerals, though..)

$ perl -le 'my @numbers = qw(
    1.c 1.a 12.g 12.a 3.z 3.1 3.b
    );
print for
    map $_->[0],
    sort { $a->[1] cmp $b->[1] } 
	map [ $_ , join "", map { sprintf"%6s", $_ } split /\./ ],
    @numbers;
'
1.a
1.c
3.1
3.b
3.z
12.a
12.g


 

-----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 Aaron
West
Sent: Friday, August 11, 2006 12:59 PM
To: 'John W. Krahn'; 'SPUG'
Subject: Re: SPUG: sorting hierarchical section numbers

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 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