jgabriel@mtecv2.mty.itesm.mx (Juan Gabriel Ruiz Pinto) (10/30/90)
I'm doing some perl scripts, but I need to sort an associative array but I want to sort it with the value of each register, I mean, if i have, $xs{"aaa"} = 4; $xs{"bbb"} = 5; $xs{"ccc"} = 2; I want to get the following results after the sort.. bbb 5 aaa 4 ccc 2 Any body has a good procedure to do that? Thank's in advance.. -- ***** Greetings from Mexico! ***** Juan Gabriel Ruiz Pinto Internet: Ing. Sistemas Electronicos jgabriel@mtecv2.mty.itesm.mx I.T.E.S.M. Campus Monterrey
merlyn@iwarp.intel.com (Randal Schwartz) (10/31/90)
In article <2524@mtecv2.mty.itesm.mx>, jgabriel@mtecv2 (Juan Gabriel Ruiz Pinto) writes: | | I'm doing some perl scripts, but I need to sort an associative | array but I want to sort it with the value of each register, | I mean, if i have, | | $xs{"aaa"} = 4; | $xs{"bbb"} = 5; | $xs{"ccc"} = 2; | | I want to get the following results after the sort.. | | bbb 5 | aaa 4 | ccc 2 | | Any body has a good procedure to do that? It's a bit hard to see what you want. A descending sort by value? That'd be something like this: ################################################## $xs{"aaa"} = 4; $xs{"bbb"} = 5; $xs{"ccc"} = 2; for $key (sort by_dec_xs_vals keys xs) { print "$key is $xs{$key}\n"; } sub by_dec_xs_vals { $xs{$a} < $xs{$b} ? 1 : -1; # $xs{$b} <=> $xs{$a} if you have it } ################################################## Like that? print "Just another descending Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel put the 'backward' in 'backward compatible'..."=========/