Re: sorting a hash / 2008-06-01



dn.perl@xxxxxxxxx wrote:
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?

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

print "City: $_->[0] Temperature: $_->[1]"
for (
sort { $b->[ 1 ] <=> $a->[ 1 ] }
map { my $hash = $_; map [ $_, $hash->{ $_ }{ max_temp } ], keys %$hash }
values %hash
)[ 0 .. 3 ];
'
City: Austin Temperature: 96
City: Boston Temperature: 96
City: San Fran Temperature: 94
City: San Jose Temperature: 84




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: Heating a City
    ... I'd like to raise the ambient temperature 20 ... Might lose lots of heat down into the Earth ... If you're only concerned about average temperature over some period, ... merely light the city on fire. ...
    (rec.arts.sf.science)
  • Re: Heating a City
    ... I'd like to raise the ambient temperature 20 ... Might lose lots of heat down into the Earth ... If you're only concerned about average temperature over some period, ... merely light the city on fire. ...
    (rec.arts.sf.science)
  • Re: [SOLUTION] Current Temperature (#68)
    ... Write a Ruby program such that given a certain argument to the program it ... will return the current temperature of that location. ... If a city is found, the search results contain a link to rss feed. ... Instead of parsing the html document, I get this rss feed and parse it. ...
    (comp.lang.ruby)
  • Re: Met Office predicting fog and min temp of 2c for Stoke-on-Trent
    ... The Met Office are predicting a minimum temperature of 2c for Stoke-on- ... city but I can't see it happening in the city itself.> ...
    (uk.sci.weather)
  • Re: Just thought you might like to know..
    ... Ice Cream Man wrote: ... your country's so small the temperature is the same everywhere? ... > shame about man city v. chelsea. ...
    (rec.music.phish)