On Mar 25, 2004, at 5:45 PM, [EMAIL PROTECTED] wrote:


On Mar 25, 2004, at 4:51 PM, [EMAIL PROTECTED] wrote:


any modules out there that can sort things such as.. BB10,
BB1100,BB11.   I want it to be in this order. BB10,BB11,BB1100.

I'm not sure about the modules, but it's generally pretty easy to roll your own sort, in Perl. See if this code gets you going:

#!/usr/bin/perl

use strict;
use warnings;

my @codes = qw(BB10 BB1100 BB11);
print "@codes\n";

my @sorted = map { join '', @$_ }       
                         sort { $a->[1] <=> $b->[1] }
                         map { m/(\D+)(\d+)/ ? [$1, $2] : ['', $_] } @codes;
print "@sorted\n";

__END__

James
James you just blew me away, can you comment some of those lines..

<laughs> Sorry, about that. Let's take it piece by piece.


map { m/(\D+)(\d+)/ ? [$1, $2] : ['', $_] } @codes

That looks uglier than it is. It just breaks the strings apart, into digit and non-digit units. I turn each one into an array ref, with the first element being the letters and the second being the digits.

sort { $a->[1] <=> $b->[1] }

Is your basic numerical sort(), except I have to index into the array refs to hit the number.

map { join '', @$_ }

And that just puts Humpty Dumpty... er, the strings back together again.

Make more sense?

James
        


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to