Home > Archive > PERL Beginners > March 2004 > sort
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| ewalker@micron.com 2004-03-26, 11:14 pm |
| any modules out there that can sort things such as.. BB10, BB1100,BB11. I want it to be in this order. BB10,BB11,BB1100.
Thanks
| |
| James Edward Gray II 2004-03-26, 11:14 pm |
| On Mar 25, 2004, at 4:51 PM, ewalker@micron.com 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
| |
| lists@kers.se 2004-03-26, 11:14 pm |
| On Thu, 25 Mar 2004 ewalker@micron.com 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.
yeah, if you have 5.8 you can use mergesort from the sorts module
use strict; use warnings; # always!
use sort '_mergesort'; # only work with perl 5.8
my $instring = "BB10,BB1100,BB11";
my @temparray = split(",",$instring);
my $outstring = join(",", sort
{ substr($a, 3, 5) cmp substr($b, 3, 5) } @temparray );
print $outstring;
__END__
--
Christian Bolstad - kers@atdt.nu, PGP key #E7BCEB9A - http://atdt.nu/
"I hate quotations." -- Ralph Waldo Emerson
| |
| James Edward Gray II 2004-03-26, 11:14 pm |
| On Mar 25, 2004, at 5:45 PM, ewalker@micron.com wrote:
>
> On Mar 25, 2004, at 4:51 PM, ewalker@micron.com wrote:
>
>
> 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
| |
| James Edward Gray II 2004-03-26, 11:15 pm |
| On Mar 25, 2004, at 6:02 PM, ewalker@micron.com wrote:
>
> James
> Yea,, now thats a ringer.. Glad you were able to look outside the
> box. :)
> now my next question is that I got say 75 different string all
> starting like the previous list.
> R50, AB11,BB110,BCD34...etc... Think this will sort the entire lot for
> me? Or should I group all the ones with the same letters at the
> beginning and sort each group, then put them back togther again.
Give it a try. It'll sort any amount, by number only. Now if you need
to sort by the letters too, it needs a small enhancement...
James
| |
| James Edward Gray II 2004-03-26, 11:15 pm |
| On Mar 25, 2004, at 6:11 PM, ewalker@micron.com wrote:
>
> James
> Hey james.. One other thing, I know its cause your a wise perl
> programmer, but how do you know on those lines not to put semi colons
> and that the data will be auto piped the next command?
map() builds up a list and returns it. sort() sorts a list and returns
it. I'm just feeding the return values directly too the next function.
It could all be on one line. Perl ignores whitespace, so I just made
it a little prettier.
James
| |
| John W. Krahn 2004-03-26, 11:15 pm |
| ewalker@micron.com 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.
Perl's built-in sort does that:
$ perl -le'@x = qw[BB11 BB10 BB1100]; print for @x; print; print for sort @x'
BB11
BB10
BB1100
BB10
BB11
BB1100
Unless you are not describing the problem correctly.
John
--
use Perl;
program
fulfillment
| |
| Bob Showalter 2004-03-26, 11:15 pm |
| ewalker@micron.com 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.
Erm, that's lexical order.
$ perl -le 'print for sort @ARGV' BB10 BB1100 BB11
BB10
BB11
BB1100
Am I missing something?
| |
| ewalker@micron.com 2004-03-26, 11:15 pm |
|
-----Original Message-----
From: Bob Showalter [mailto:Bob_Showalter@taylorwhite.com]
Sent: Friday, March 26, 2004 12:19 PM
To: ewalker; beginners@perl.org
Subject: RE: sort
ewalker@micron.com 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.=20
Erm, that's lexical order.
$ perl -le 'print for sort @ARGV' BB10 BB1100 BB11
BB10
BB11
BB1100
Am I missing something?
Thanks all, I as able to get it to sort properly.
| |
| Jenda Krynicky 2004-03-26, 11:15 pm |
| From: lists@kers.se
> On Thu, 25 Mar 2004 ewalker@micron.com wrote:
>
>
> yeah, if you have 5.8 you can use mergesort from the sorts module
>
> use strict; use warnings; # always!
> use sort '_mergesort'; # only work with perl 5.8
>
> <snipped>
>
> --
> Christian Bolstad - kers@atdt.nu
Erm ... why?
There is no point in using the sort pragma/module in this case.
Unless you either need the sort() to be stable (A stable sort means
that for records that compare equal, the original input ordering is
preserved.) you should not need to use it.
There is very seldom any reason to force either mergesort or
quicksort.
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
|
|
|
|
|