Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-07-11 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (222 votes), past polls