Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uunet!orca!javelin.sim.es.com!pashdown From: pashdown@javelin.sim.es.com (Pete Ashdown) Newsgroups: comp.lang.perl Subject: Sorting an associative array by value? Message-ID: <1991Apr5.183701.21325@javelin.sim.es.com> Date: 5 Apr 91 18:37:01 GMT Organization: Evans & Sutherland Computer Corp., Salt Lake City, Utah Lines: 24 How do I go about sorting an associative array, not by the string, but by the associated value? For example, how would I change following 'word count' program to sort by occurance rather than alphabetically? (From the Nutshell Book) while (<>) { s/-\n//g; tr/A-Z/a-z/; @words = split (/\W*\s+\W*/, $_); foreach $word (@words) { $wordcount {$word}++; } } foreach $word (sort keys(%wordcount)) { printf "%20s %d\n", $word, $wordcount{$word}; } -- / "Uhh" - Jon Bon Jovi \ HARHARHARHARHARHARHARHARHAR \\ / AMGIA!! The computer for the mind. \X v// HAR(This space for rent)HAR |XV [FREE CD'S! SEND ME MONEY!] X\/ HARHARHARHARHARHARHARHARHAR Pete Ashdown pashdown@javelin.sim.es.com ...uunet!javelin.sim.es.com!pashdown