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 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: Similarity searching
    ... The usual duplicate finder is a sort followed by comparison of the adjacent items in the sorted list. ... Generating a hash function for N elements is O. ... A 64 bit hash function takes about twice as long to compute, ... Search terms include nearest neighbor, range searching, partial matching and k-d trees. ...
    (comp.lang.fortran)
  • 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)
  • Re: Sorted Hash
    ... eliminating or improving the sort has about the same potential as ... making the sort the prime candidate for optimization. ... Tie::IxHash keeps the hash sorted, but has quite a lot of overhead. ... in sorted order you could just keep the sorted keys around instead of ...
    (comp.lang.perl.misc)