[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cgi-list
Subject:    Re: [CGI] Sorting a hash by its values
From:       Terry Brown <Terry.Brown () ncl ! ac ! uk>
Date:       1999-03-19 16:06:04
[Download RAW message or body]

At 09:20 19/03/99, Rich wrote:
>I have a hash that looks like this:
>
>%data = (
>"item1" => "12"
>"item2" => "12"
>"item3" => "5"
>"item4" => "22"
>"item5" => "2"
>)
>
>I want to create a new array sorted numerically has that only has the the
>top three items like this :
>%newdata =(
>"item4" => "22"
>"item1" => "12"
>"item2" => "12"
>)
>
>I'm getting stuck at sorting the hash by it's values.
>
>I am able to create an array of the sorted values sorted like this:
>@sortedvalues = sort{$a <=> $b}(values(%data));
>

Hi Rich,

Hashes (as far as I understand them) have no real order and it's what you do
with them that gives them order.

If you only want to access the top three values of the %data hash, you can do:

my($count) = 0;

foreach $value (sort {$b <=> $a} values %data) {
        print ("$value\n");
        $count++;
        last if ($count == 3);
}


If however, you just want the new hash with the top three values (knowing
you'll have to do some sort routine if you want access to the values in
descending order) then you could do this:

@values = (sort {$data{$b} <=> $data{$a}} keys %data);

my($count) = 0;

foreach $key (@values) {
        $newdata{$key} = $data{$key};
        $count++;
        last if ($count == 3);
}


You now have %newdata with key values:

item1 - 12
item2 - 12
item4 - 22

and you can sort it to your hearts content :-)

Hope this helps,
Tez


>I figured I would then reverse %data and use @sortedvalues as the keys but
>there are duplicate keys.
>
>At  this point I am lost.  There seems to be a solution in the Perl Cookbook
>but there must be a typo.
>
>
>Help !
>
>TIA,
>Rich
>
>-----------------------------------------------------------------
>To unsubscribe, send mail to "majordomo@jann.com" with "unsubscribe cgi-list"
>in the body.
>
>The CGI Tips & Tricks website (and archive of the list) is located
>at http://perl.jann.com/
>
>cgi-list is hosted by Summit Communication Design 
>(http://www.summitdesign.net).
>
>Summit Communication Design is a full-service web hosting firm
>providing web design, cgi programming and inter/intra-net support.
>-----------------------------------------------------------------


Terry Brown			http://numedsun.ncl.ac.uk/
C&IT Development Officer
Faculty of Medicine Computing Centre
University of Newcastle, NE2 4HH
Tel: +44 191 222 5116	Fax: +44 191 222 5016

PGP: 22A4 6205 0F2D 9DD7 5614  DD77 99AD FAD9 D766 E18F
-----------------------------------------------------------------
To unsubscribe, send mail to "majordomo@jann.com" with "unsubscribe cgi-list"
in the body.

The CGI Tips & Tricks website (and archive of the list) is located
at http://perl.jann.com/

cgi-list is hosted by Summit Communication Design (http://www.summitdesign.net).

Summit Communication Design is a full-service web hosting firm
providing web design, cgi programming and inter/intra-net support.
-----------------------------------------------------------------

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic