LPM: The times they are a-changin' (or: integer hash keys)
Janine
janine at emazing.com
Tue Aug 21 16:26:00 CDT 2001
What about this:
#!/usr/bin/perl -w
use strict;
my %nums = (
1 => 'one',
2 => 'two',
10 => 'ten',
20 => 'twenty'
);
print "The old way...\n";
foreach my $num (sort keys %nums) {
print "$num ($nums{$num})\n";
}
print "\n\n";
print "The new way...\n";
#my @sorted = sort ( map { sprintf("%012u",$_) } (keys %nums) );
#my @sorted = sort @mapped;
foreach my $num (sort ( map { sprintf("%012u",$_) } (keys %nums) )) {
my $orig = int $num;
print "$num ($nums{$orig})\n";
}
Janine
> -----Original Message-----
> From: owner-lexington-pm-list at pm.org
> [mailto:owner-lexington-pm-list at pm.org]On Behalf Of Paul Stackhouse
> Sent: Tuesday, August 21, 2001 5:15 PM
> To: lexington-pm-list at happyfunball.pm.org
> Subject: Re: LPM: The times they are a-changin' (or: integer
> hash keys)
>
>
> It appears to only be a problem on the exact day of the
> cross-over. Once you
> get to the ninth, when all of the entries are ten digits,
> everything sorts
> properly again, right? I'm still curious, though, about the
> answer to your
> question.
>
> -paul
>
> David Hempy wrote:
> >
> >
> > Well, since Y2K was such a fizzle, we get another chance at
> notoriety on
> > September 8, 2001, when we exceed 999,999,999 seconds since the
> > epoch. Shouldn't be a big deal, as that's only one less
> than 1,000,000,000
> > seconds.
> >
> > But yet, I'm facing it in one of my programs...
> >
> > I got a call from a user complaining that September 8th's
> program schedule
> > was out of order. Everything after 9:30 PM (EDT) showed up
> first, before
> > our first show at 8:00 AM. Sure enough, she was right:
> >
> >
http://edit-www.ket.org/cgi-plex/schedule/ribbon_funnytime.pl?date=2001-
09-08
>
> It didn't take too long to figure notice the coincidence between the
> sorting problem and the 9+1 issue. After convincing myself that SQL
Server
> didn't know how to sort these values correctly (duh) I looked at my
own code.
>
> Turns out the problem was actually me not understanding how hash keys
> work. I was using epoch times as hash keys. It appears that hash
keys are
> stored as strings, not the integers I was expecting. For example:
>
> $foo{1} = "one";
> $foo{2} = "two";
> $foo{3} = "three";
> $foo{10} = "ten";
> $foo{20} = "twenty";
> $foo{30} = "thirty";
>
> foreach my $key (sort keys %foo) {
> print "$key - $foo{$key}\n";
> }
>
>
> This produces this output:
> 1 - one
> 10 - ten
> 2 - two
> 20 - twenty
> 3 - three
> 30 - thirty
>
> Any suggestions on a good way to sort hash keys by their integer
values?
>
> -dave
>
> --
> David Hempy
> Internet Database Administrator
> Kentucky Educational Television - Distance Learning Division
> <hempy at ket.org> -- (859)258-7164 -- (800)333-9764
--
Paul Stackhouse Webmaster
Kentucky Educational TV Voice: 859-258-7135
600 Cooper Drive Fax: 859-258-7399
Lexington, KY 40502 http://www.ket.org
More information about the Lexington-pm
mailing list