Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Sort array + keep associated indexes in other array

by Eliya (Vicar)
on Mar 28, 2012 at 13:17 UTC ( #962180=note: print w/ replies, xml ) Need Help??


in reply to Sort array + keep associated indexes in other array

You can compute an appropriately sorted list of indices, and then reorder both arrays using those indices:

#!/usr/bin/perl -l my @array_items = ("dog", "desk" ,"cow"); my @array_items_weight = ("20" , "10", "150"); my @i = sort { $array_items[$a] cmp $array_items[$b] } 0..$#array_item +s; @array_items = @array_items[@i]; @array_items_weight = @array_items_weight[@i]; print "@array_items"; # cow desk dog print "@array_items_weight"; # 150 10 20


Comment on Re: Sort array + keep associated indexes in other array
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (16)
As of 2014-08-27 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls