RE: sort with special order
How about this? Jo #!/usr/bin/perl use strict; use warnings; my @list = qw/dog is a there/; my @sortOrder = (3,1,2,0); my @sorted; foreach (@sortOrder) { push(@sorted,$list[$_]); } print @sorted; exit; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: sort with special order
Jo for lists and groups wrote: How about this? Jo #!/usr/bin/perl use strict; use warnings; my @list = qw/dog is a there/; my @sortOrder = (3,1,2,0); my @sorted; foreach (@sortOrder) { push(@sorted,$list[$_]); } print @sorted; exit; ...or this: #!/usr/bin/perl use strict; use warnings; my @list = map { qw(dog is a there)[$_]; } qw(3 1 2 0); print @list; ... or all at once: print join(' ', my @list = map { qw(dog is a there)[$_]; } qw(3 1 2 0)); :) Steve smime.p7s Description: S/MIME Cryptographic Signature
Re: sort with special order
Steve Bertrand wrote: Jo for lists and groups wrote: How about this? Jo #!/usr/bin/perl use strict; use warnings; my @list = qw/dog is a there/; my @sortOrder = (3,1,2,0); my @sorted; foreach (@sortOrder) { push(@sorted,$list[$_]); } print @sorted; exit; ...or this: #!/usr/bin/perl use strict; use warnings; my @list = map { qw(dog is a there)[$_]; } qw(3 1 2 0); print @list; Of course, it would likely be required to have the data in arrays first, so this is likely more feasible: my @list = qw (dog is a there); my @order = qw (3 1 2 0); @list = map { $list[$_]; } @order; print @list; smime.p7s Description: S/MIME Cryptographic Signature
Re: sort with special order
On Fri, Jul 24, 2009 at 11:01, Jo for lists and groupsourli...@rogers.com wrote: How about this? Jo #!/usr/bin/perl use strict; use warnings; my @list = qw/dog is a there/; my @sortOrder = (3,1,2,0); my @sorted; foreach (@sortOrder) { push(@sorted,$list[$_]); } print @sorted; exit; snip What do you do when the list is qw/there a dog is/? Your code does not sort the data. I guess you could go insane and write something like: my %position = ( there = 0, is= 1, a = 2, dog = 3, ); for my $word (@list) { $sorted[$position{$word}] = $word; } But that would only work if the list will only contain one item each of there, is, a, or dog. The sort is not only more flexible, it is faster (due to the low number of items to be sorted and the fact that sort is implemented in C): positional_hash = there is a dog sort = there is a dog positional_array = there is a dog Rate positional_hash positional_array sort positional_hash 253734/s -- -11% -15% positional_array 284350/s 12% -- -5% sort 297890/s 17% 5% -- #!/usr/bin/perl use strict; use warnings; use List::Util qw/maxstr/; use Benchmark; my %position = ( there = 0, is= 1, a = 2, dog = 3, ); my @sortOrder = (3,1,2,0); my @a = qw/dog is a there/; my %subs = ( sort = sub { return join , sort { $position{$a} = $position{$b} } @a; }, positional_array = sub { my @sorted; push @sorted, $a[$_] for @sortOrder; return join , @sorted; }, positional_hash = sub { my @sorted; $sorted[$position{$_}] = $_ for @a; return join , @sorted; }, ); for my $sub (keys %subs) { print $sub = , $subs{$sub}-(), \n; } Benchmark::cmpthese -1, \%subs; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: sort with special order
On Fri, Jul 24, 2009 at 00:14, sys admsys...@computermail.net wrote: Hi, When I got a word list, I want it to be sorted with special order. for example, I got this array: (dog,is,a,there); I want the sorted result is: (there,is,a,dog); How to code it? Thank you. snip You need to some way to map the list to be sorted into something that can be sorted. When you hear the phrase map X into Y, you should think of a hash: #!/usr/bin/perl use strict; use warnings; my %sort_order = ( there = 1, is= 2, a = 3, dog = 4, ); my @list = qw/dog is a there/; my @sorted = sort { $sort_order{$a} = $sort_order{$b} } @list; print list:\n, (map { \t$_\n } @list), sorted\n, map { \t$_\n } @sorted; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: sort with special order
On Oct 3, 2005, at 17:35, JupiterHost.Net wrote: I need to sort them so they are in order of Q, B , then Z The real array will have between 1 to 100 items, just FYI They all go in ASCII relative order except B - Q, thus a way to get it is to handle that special case and delegate to cmp the rest: my @sorted = sort { my $x = substr($a, 0, 1) . substr($b, 0, 1); $x eq BQ || $x eq QB ? $b cmp $a : $a cmp $b; } @array; I used the concatenation for clarity, you see the idea anyway if that's too expensive for your usage. -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: sort with special order
On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: [SPAM DETECT] Re: sort with special order
On Oct 3, 2005, at 18:16, Jeff 'japhy' Pinyan wrote: On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; There's a potential gotcha there: since all Qs and Bs are being swapped lexicographic order after the first character is also changed. That might not be an issue though. -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: sort with special order
On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( Jeff 'japhy' Pinyan wrote: I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; Xavier Noria wrote: They all go in ASCII relative order except B - Q, thus a way to get it is to handle that special case and delegate to cmp the rest: my @sorted = sort { my $x = substr($a, 0, 1) . substr($b, 0, 1); $x eq BQ || $x eq QB ? $b cmp $a : $a cmp $b; } @array; I used the concatenation for clarity, you see the idea anyway if that's too expensive for your usage. Brilliant! I'll benchmark those ideas :) Thanks you two! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: [SPAM DETECT] Re: sort with special order
On Oct 3, Xavier Noria said: On Oct 3, 2005, at 18:16, Jeff 'japhy' Pinyan wrote: my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; There's a potential gotcha there: since all Qs and Bs are being swapped lexicographic order after the first character is also changed. That might not be an issue though. My code relies on the sample data being indicative of the rest of the data, meaning there'll only be a capital Q, B, or Z at the beginning of the string. -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: sort with special order
JupiterHost.Net wrote: On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( Jeff 'japhy' Pinyan wrote: I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; Xavier Noria wrote: They all go in ASCII relative order except B - Q, thus a way to get it is to handle that special case and delegate to cmp the rest: my @sorted = sort { my $x = substr($a, 0, 1) . substr($b, 0, 1); $x eq BQ || $x eq QB ? $b cmp $a : $a cmp $b; } @array; I used the concatenation for clarity, you see the idea anyway if that's too expensive for your usage. Brilliant! I'll benchmark those ideas :) Thanks you two! Read this for an explanation of Jeff's solution: http://en.wikipedia.org/wiki/Schwartzian_transform -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: sort with special order
On 10/3/05, Bakken, Luke [EMAIL PROTECTED] wrote: JupiterHost.Net wrote: On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( Jeff 'japhy' Pinyan wrote: I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; Xavier Noria wrote: They all go in ASCII relative order except B - Q, thus a way to get it is to handle that special case and delegate to cmp the rest: my @sorted = sort { my $x = substr($a, 0, 1) . substr($b, 0, 1); $x eq BQ || $x eq QB ? $b cmp $a : $a cmp $b; } @array; I used the concatenation for clarity, you see the idea anyway if that's too expensive for your usage. Brilliant! I'll benchmark those ideas :) Thanks you two! Read this for an explanation of Jeff's solution: http://en.wikipedia.org/wiki/Schwartzian_transform Sort of. Perhaps Randy will chime in here, but as I understand it, the key to the Schwartzian transform is that the input data 1) doesn't undergo direct comparison, and 2) is passed by reference, speeding up the sort, so: my @sorted = map { $_-[0] } sort { $a-[1] cmp $b[1] } map { [$_, s/^(.)/$1 eq Q ? 1 : ($1 eq B ? 2 : ($1 eq Z ? 3 : $1))/ex] } @original; That will slow you down a little with the capturing and substitution, but is solves Jeff's problem with capital Q, B, or Z later in the string. I'm not sure how it stacks up to Xaiver's temp variable and multiple substr calls. This isn't as simple as it looked. HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!
Re: sort with special order
Bakken, Luke wrote: JupiterHost.Net wrote: On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( Jeff 'japhy' Pinyan wrote: I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; Brilliant! I'll benchmark those ideas :) Thanks you two! Read this for an explanation of Jeff's solution: http://en.wikipedia.org/wiki/Schwartzian_transform Actually Jeff's solution uses the Guttman-Rosler transform: http://www.sysarch.com/Perl/sort_paper.html John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: sort with special order
[sorry, PINE has become very confused about who said what] On Oct 3, Jay Savage said: On 10/3/05, Bakken, Luke [EMAIL PROTECTED] wrote: JupiterHost.Net wrote: On Oct 3, JupiterHost.Net said: I have a list of strings that start with an uppercase B, Q, or Z I need to sort them so they are in order of Q, B , then Z Any ideas or input on how to efficiently do that with sort() or even map() is most appreciated :) perldoc -f sort|-f map didn't appear to address this situation :( Jeff 'japhy' Pinyan wrote: I would use map() before and after sort() to correct leading characters. my @sorted = map { tr/123/QBZ/; $_ } sort map { tr/QBZ/123/; $_ } @data; Xavier Noria wrote: They all go in ASCII relative order except B - Q, thus a way to get it is to handle that special case and delegate to cmp the rest: my @sorted = sort { my $x = substr($a, 0, 1) . substr($b, 0, 1); $x eq BQ || $x eq QB ? $b cmp $a : $a cmp $b; } @array; I used the concatenation for clarity, you see the idea anyway if that's too expensive for your usage. Brilliant! I'll benchmark those ideas :) Thanks you two! Read this for an explanation of Jeff's solution: http://en.wikipedia.org/wiki/Schwartzian_transform That will slow you down a little with the capturing and substitution, but is solves Jeff's problem with capital Q, B, or Z later in the string. I'm not sure how it stacks up to Xaiver's temp variable and multiple substr calls. This isn't as simple as it looked. Actually, I'm using a Guttman-Rosler Transform, not a Schwartzian Transform. I've actually come up with a slightly less dangerous technique I believe: my @sorted = map substr($_, 1), sort map +{qw( Q a B b Z c )}-{substr($_, 0, 1)} . $_, @original; Reading from the bottom up: 1. we have the original array 2. using an anonymous hash reference ({Q = 'a', B = 'b', Z = 'c'}), we select the first character (Q, B, or Z) of the string ($_) and prepend the value in the hash corresponding that character (a for Q, b for B, or c for Z) 3. we then sort the modified list of words, with all the 'Q' words being first (since they are now prepended with 'a'), then all the 'B' words, then all the 'Z' words 4. then we extract everything AFTER the first character -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response