how to sort two array in perl

how to sort two array in perl

am 13.10.2011 11:39:52 von Lemon

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.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to sort two array in perl

am 14.10.2011 16:32:09 von Shlomi Fish

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

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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to sort two array in perl

am 14.10.2011 16:38:08 von Rob Coops

--20cf303ea6a4db2c0504af4335d8
Content-Type: text/plain; charset=UTF-8

On Fri, Oct 14, 2011 at 4:32 PM, Shlomi Fish wrote:

> On Thu, 13 Oct 2011 02:39:52 -0700 (PDT)
> 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.
> >
>
> 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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
Another good source for explanations and a lot of answers to questions you
are sure to have at some point in time:
http://www.perlmonks.org/?node_id=15209

This one deals exactly with your question and suggests the same solution as
Shlomi does but spends a little bit more time on explaining this. (the link
Shlomi posted explains this as well of course ;-)

Regards,

Rob

--20cf303ea6a4db2c0504af4335d8--

Re: how to sort two array in perl

am 14.10.2011 17:16:53 von Paul Johnson

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 - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to sort two array in perl

am 15.10.2011 09:45:32 von Lemon

--20cf3005120820669004af5190b7
Content-Type: text/plain; charset=UTF-8

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 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 - paul@pjcj.net
> http://www.pjcj.net
>

--20cf3005120820669004af5190b7--

Re: how to sort two array in perl

am 16.10.2011 15:54:08 von Shawn Wilson

On Sat, Oct 15, 2011 at 03:45, Lemon wrote:
> Many thanks to Shlomi and Paul,
>

>
> I need to read sort manual and map manual.
>

yes, that's a good place to start.

but, just because i like being different and because i really enjoy
the power of this simple little module:
perl -e 'use List::UtilsBy qw/sort_by/; @arr = qw/4 5 3 6 2/; @sorted
= sort_by { $_ } @arr; print "@sorted\n";'

oh, and because LeoNerd wrote it and because it does what you want
with less typing :)

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/