Click here to get back home

sorting a hash / 2008-06-01

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sorting a hash / 2008-06-01 dn.perl@gmail.com 05-30-2008
Get Chitika Premium
Posted by Dave Saville on June 12, 2008, 5:18 am
Please log in for more thread options
wrote:

>
> > On Fri, 30 May 2008 13:11:22 UTC, Gunnar Hjalmarsson
> >
> > > dn.perl@gmail.com wrote:
> > > > I want to sort a hash. The hash contains a list of cities and their
> > > > temperature
> > >
> > > Well, I'd rather say it contains three hash references.
> > >
> > > This is one sensible way to sort that data structure:
> > >
> > > foreach my $state ( sort keys %hash ) {
> > > print "State: $state\n";
> > > foreach my $city ( sort { $a cmp $b } keys %{ $hash } ) {
> > > print "$city = $hash\n";
> > > }
> > > print "\n";
> > > }
> >
> >
> > Sorry to jump in with another question but I have a very similar
> > problem. I am processing a consolidated apache2 logfile. I have
> > multiple virtual hosts. All I care about are the site, the page
> > served, a counter and the date.
> >
> > So my hash looks like $urls Beyond that I have a counter
> > and date thus:
> > $urls[0]++; # count
> > $urls[1] = $date;
> >
> > This works fine and I can list by site the page, count and date.
> >
> > foreach $site ( keys %urls)
> > {
> > foreach my $url (keys %})
> > {
> > print "$site $url $urls[0] $urls[1]\n";
> > }
> > }
> >
> > Putting a sort into the url loop gives me the results sorted by page
> > as expected. What I cannot figure out is how to do it by count and by
> > date.
> >
> > I have tried various ideas I found by google but they all tend to be
> > similar to this
> >
> > sub by_count
> > {
> > $urls[0] <=> $urls[0] or $a cmp $b;
> > }
> >
> > But this throws lots of "Use of uninitialized value....." errors on
> > that line and in doing so gets the wrong pages attributed to a site. I
> > have tried with yet another hash on the end with count & date keys
> > instead of the array, but it does not help.
> >
> > I would be grateful for any pointers.
>
> Are you trying to sort all of your records at once, regardless of site?
> You can't do that with a simple sort. If you just want to sort the data
> by site, here is an example:
<snip>

Thanks Jim. No, one site's ouput needs sorting by date and the rest by
count. I have found the problem, reported in another reply, but
basically my version of perl treats

sort { some sort statement} keys %hash

and

sort by_value keys %hash

where by_value contains the exact same sort statement differently.

--
Regards
Dave Saville

NB Remove nospam. for good email address

Posted by sheinrich on May 30, 2008, 5:52 pm
Please log in for more thread options

...
>
> my %hash = () ;
> $hash{San Jose} = 84 ;
> $hash{San Fran} = 94 ;
> $hash = 38 ;
> $hash = 66 ;
> $hash = 72 ;
> $hash = 96 ;
> $hash{Fort Worth} = 62 ;
> $hash = 96 ;
> $hash = 55 ;
> $hash = 55 ;
>
Excuse me, but is it really absolutely unthinkable to share your
information with the meager 99% percent of the world that has adopted
SI units decades(!) ago?

https://www.cia.gov/library/publications/the-world-factbook/appendix/appendix-g.html

Cheers, Steffen

Posted by Dan Rumney on June 1, 2008, 8:04 pm
Please log in for more thread options
> Excuse me, but is it really absolutely unthinkable to share your
> information with the meager 99% percent of the world that has adopted
> SI units decades(!) ago?
>
>
https://www.cia.gov/library/publications/the-world-factbook/appendix/appendix-g.html

Since the US uses Fahrenheit for its temperatures and these are all US
cities, then Fahrenheit does seem like an appropriate choice, don't you
think?

Also, since the script is clearly about the average temperature with
regards to the weather, the choice of units is hardly a *critical* one
and so widespread local convention would seem to be the sensible choice.

Posted by Jürgen Exner on June 1, 2008, 8:27 pm
Please log in for more thread options
>> Excuse me, but is it really absolutely unthinkable to share your
>> information with the meager 99% percent of the world that has adopted
>> SI units decades(!) ago?
>>
>>
https://www.cia.gov/library/publications/the-world-factbook/appendix/appendix-g.html
>
>Since the US uses Fahrenheit for its temperatures and these are all US
>cities, then Fahrenheit does seem like an appropriate choice, don't you
>think?

Well, very much depends on your target audience. If you never ever will
use that program/that data outside of the Fahrenheit enclave, then using
Fahrenheit is quite acceptable.

However if there is even the tiniest chance that this program/this data
is ever to be used outside of the Fahrenheit island then you should
consider international standards right from the beginning. Believe me,
de-localizing code and making it global is a _MAJOR_ pain. It is much
worse than writing globalized code right from the get-go. Been there,
done that for many years.

>Also, since the script is clearly about the average temperature with
>regards to the weather, the choice of units is hardly a *critical* one
>and so widespread local convention would seem to be the sensible choice.

'Widespread' and 'local' is kind of a contradiction ;-))

jue

Posted by Dan Rumney on June 1, 2008, 9:44 pm
Please log in for more thread options
[snip]
>> Since the US uses Fahrenheit for its temperatures and these are all US
>> cities, then Fahrenheit does seem like an appropriate choice, don't you
>> think?
>
> Well, very much depends on your target audience. If you never ever will
> use that program/that data outside of the Fahrenheit enclave, then using
> Fahrenheit is quite acceptable.
>
> However if there is even the tiniest chance that this program/this data
> is ever to be used outside of the Fahrenheit island then you should
> consider international standards right from the beginning. Believe me,
> de-localizing code and making it global is a _MAJOR_ pain. It is much
> worse than writing globalized code right from the get-go. Been there,
> done that for many years.

I agree in principle that code that deals with human measurement units
should *at least* be aware of the fact that there are different
measurement systems. As a general rule, I would defer to your wider
experience (I infer from your name that you're European, so you're
probably a little more exposed to issues of internationalisation than US
developers)

However, in the particular case of temperature and weather, there really
are only two measurement systems: Fahrenheit and Celcius (or Centigrade,
if you prefer). Since a pragmatic script that is internationalized would
allow the user to choose their unit of choice for display, it's academic
which system is used inside the Perl script... this could be easily
hidden from the calling script by a wrapper function performing the
necessary conversions.

Heck, you could store the temperatures in Kelvin, the true SI unit for
temperature and convert to C or F as required by the calling function :o)

>
>> Also, since the script is clearly about the average temperature with
>> regards to the weather, the choice of units is hardly a *critical* one
>> and so widespread local convention would seem to be the sensible choice.
>
> 'Widespread' and 'local' is kind of a contradiction ;-))

*blush* good point. Perhaps 'Widely-accepted' and 'regional' would have
been better choices

Similar ThreadsPosted
Sorting a hash containing a hash of hashes December 14, 2005, 2:29 pm
Hash Sorting June 14, 2005, 2:49 pm
Sorting Hash by Value and Key May 17, 2007, 9:57 am
Sorting on sub-hash values June 23, 2005, 11:30 am
Sorting AofH over hash key(s)... October 30, 2007, 4:40 pm
Nested sorting of a hash December 6, 2007, 6:23 am
warnings on sorting hash of hashes January 5, 2005, 11:53 pm
sorting data - hash vs. list September 11, 2005, 4:41 pm
Sorting array of hash references October 26, 2006, 6:21 am
Sorting "string" numerical keys from a hash. September 5, 2004, 2:47 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap