On Thu, 13 Oct 2011 02:39:52 -0700 (PDT)
Lemon <[email protected]> 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.
> 

If the data set is an array of array references you can use the "||" to chain
comparisons:

[CODE]
my @sorted = sort { ($a->[0] <=> $b->[0]) || ($a->[1] <=> $b->[1]) } @a_and_b;
[/CODE]

If the two data sets are kept as separate arrays you can sort an array of
indexes:

[CODE]
my @sorted_indexes = sort { ($arr1[$a] <=> $arr1[$b]) || ($arr2[$a] <=>
$arr2[$b]) } (0 .. $#arr1);
[/CODE]

For more information see:

http://perl-begin.org/tutorials/perl-for-newbies/part4/#page--and_or--sort--PAGE

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Escape from GNU Autohell - http://www.shlomifish.org/open-source/anti/autohell/

There is no IGLU Cabal! None of them could pass the Turing test. But strangely
enough a computer program they coded, could.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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


Reply via email to