sorting a hash / 2008-06-01




I want to sort a hash. The hash contains a list of cities and their
temperature and I want the 4 cities with max temp. The problem is that
the city-names are one extra level deep with the state-name coming in-
between. I wondered whether I should build the hash differently. A
different format would be: state_city, with the underbar separating
the state and the city.
$hash{Calif_Cupertino}{max_temp} = 38 ;
instead of
$hash{Calif}{Cupertino}{max_temp} = 38 ;


my %hash = () ;
$hash{Calif}{San Jose}{max_temp} = 84 ;
$hash{Calif}{San Fran}{max_temp} = 94 ;
$hash{Calif}{Cupertino}{max_temp} = 38 ;
$hash{Calif}{Fremont}{max_temp} = 66 ;
$hash{Texas}{Dallas}{max_temp} = 72 ;
$hash{Texas}{Austin}{max_temp} = 96 ;
$hash{Texas}{Fort Worth}{max_temp} = 62 ;
$hash{Mass}{Boston}{max_temp} = 96 ;
$hash{Mass}{Framingham}{max_temp} = 55 ;
$hash{Mass}{Worcester}{max_temp} = 55 ;

How do I sort this hash, please?
Thanks in advance.



.



Relevant Pages

  • Re: help on hasah usage
    ... PERL Learner wrote: ... Using hash, how can I add up all the population of different cities ... within one state at a time and then sort out all the states in ...
    (perl.beginners)
  • Re: Sorting columns in a Transform/crosstab query
    ... Hash: SHA1 ... sort the column headers by project name within a project type. ... You can't sort the PIVOT values and it doesn't matter if the source is ...
    (microsoft.public.access.queries)
  • Re: OT: telephone black box
    ... Jim Thompson wrote: ... ...but that isn't nearly as fun for the programmer weenies as arguing about sort algorithms and hash tables. ... A simple an instant check for no name and/or no number for magic 3-tone and hangup, next simple comparison scan in blacklist for same response on match, THEN simple comparison scan in whitelist for passthru, FINALLY failing all of that, simple recorded message similar to "please leave name and number" and hangup after message receipt)3 min max). ...
    (sci.electronics.design)
  • Re: Sorting a Hash of Arrays by an Element in the Array
    ... > Alf McLaughlin wrote: ... >> element of the array will be sorted in. ... > Your example would only sort numbers correctly. ... but since you can always sort a hash who really cares? ...
    (comp.lang.perl.misc)
  • Re: FAQ 4.41 How can I remove duplicate elements from a list or array?
    ... It's a hash. ... You seem to be under the very bizarre impression that "bubble sort" is ... than to introduce students to the concept of sorting. ... the problem then is in the lookup. ...
    (comp.lang.perl.misc)