Many thanks to Shlomi and Paul,

Shlomi gave the solution  for @array[@a,@b] (@a=(1,  7, 54,  2), @b(2, 89,
78, 33) ),  for my case two array may be esay to deal with.

I need to read sort manual and map manual.

Meng Ni


On Fri, Oct 14, 2011 at 11:16 PM, Paul Johnson <[email protected]> wrote:

> 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
>

Reply via email to