Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!sdd.hp.com!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: sorting associative arrays Keywords: perl, arrays Message-ID: <1990Oct30.163709.26412@iwarp.intel.com> Date: 30 Oct 90 16:37:09 GMT References: <2524@mtecv2.mty.itesm.mx> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: comp Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 43 In-Reply-To: jgabriel@mtecv2.mty.itesm.mx (Juan Gabriel Ruiz Pinto) 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'..."=========/