Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: sorting two arrays together

by furry_marmot (Pilgrim)
on Dec 23, 2010 at 22:13 UTC ( #878892=note: print w/ replies, xml ) Need Help??


in reply to sorting two arrays together

It can be done in one step, using an index. This leaves the original untouched, in the manner the OP requested:

use strict; my @a = qw (92 6 2); my @b = qw (60 5 12); my $i=0; my @c = map { $_->[0], $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ $a[$i], $b[$i++] ] } @a; print join ' ', @c , "\n"; ## Prints 2 12 6 5 92 60

Cheers!

--marmot


Comment on Re: sorting two arrays together
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://878892]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2015-07-07 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls