On Thu, Oct 13, 2011 at 02:39:52AM -0700, Lemon wrote:
> Dear all,
> 
> I want to sort data set like this
> 
> (@a, @b)
> 1,2                                            1,2
> 7,89                        =>              2,33
> 54,78                                        7,89
> 2,33                                           54,78
> 
> 
> I know that linux command sort can do these kind of things by "sort -
> k1,1n", but  how can I do it by perl?     I could not use hash because
> the first row may be repeat.

Depending on what exactly you need, the following may be of some help:

#!/usr/bin/perl

use strict;
use warnings;

my @a = (1,  7, 54,  2);
my @b = (2, 89, 78, 33);

my $l = [ sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] }
               map [ $a[$_], $b[$_] ], 0 .. $#a ];
@a = map $_->[0], @$l;
@b = map $_->[1], @$l;

-- 
Paul Johnson - [email protected]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to