Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot

Links

XODOX
Impressum

#1: how to sort two array in perl

Posted on 2011-10-13 11:39:52 by 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/

Report this message

#2: Re: how to sort two array in perl

Posted on 2011-10-14 16:32:09 by Shlomi Fish

On Thu, 13 Oct 2011 02:39:52 -0700 (PDT)
Lemon <lemon027@gmail.com> 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/

Report this message

#3: Re: how to sort two array in perl

Posted on 2011-10-14 16:38:08 by Rob Coops

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

On Fri, Oct 14, 2011 at 4:32 PM, Shlomi Fish <shlomif@shlomifish.org> wrote:

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

Report this message

#4: Re: how to sort two array in perl

Posted on 2011-10-14 17:16:53 by 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/

Report this message

#5: Re: how to sort two array in perl

Posted on 2011-10-15 09:45:32 by 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 <paul@pjcj.net> 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--

Report this message

#6: Re: how to sort two array in perl

Posted on 2011-10-16 15:54:08 by Shawn Wilson

On Sat, Oct 15, 2011 at 03:45, Lemon <lemon027@gmail.com> 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/

Report this message