Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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 studying the Monastery: (9)
As of 2014-12-21 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (105 votes), past polls