Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://962180]
[stevieb]: I have fixed a boatload of issues across several of my distributions, including a couple that have been nagging for a very long time and were exceptionally difficult to solve. Saving $ before we move, so thought I'd have a code session...
[stevieb]: ...session weekend. Went far better than I could have imagined. Even got a bunch of stuff soldered for another one of my electronics projects on the go, and am working on a fix for a MetaCPAN::Client issue I found
[shmem]: the smaller your boat is, the smaller is the load...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-06-25 22:24 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (571 votes). Check out past polls.