Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a time column which looks like 2005-02-01 14:35:24. I need to break this apart into the hour of the day which has already been done with my regex $hours =~ /\s(\d\d)/;.
After we have the hours we need it placed into a hash so we can count how many hours existed and how many times each hour has been updated. The way I have it setup has an output of like 4 hours (which works perfectly for those 4 hours but I'd like a way for every other hour to be defaulted to 0 so all the hours get printed). So in short, I need to extract the hours and calculate how many times that hour has been found and set all of the ones not used to 0. This is the part that confuses me and the only way I can think of is creating the hash manually and setting each one to 0 which I will do if there's no easy way to go about doing it.
Thanks for your help.
Live outputmy @hours; # so we have a permanent variable with our data while (my @row = $sth->fetchrow_array) { push(@hours, $_) for @row } my %byhour; foreach my $hours (@hours) { $hours =~ /\s(\d\d)/; my $knownhour = $1; ########## # enter hours into our hash for counting ########## if (exists $byhour{$knownhour}) { my $hourcount = $byhour{$knownhour}; $hourcount++; $byhour{$knownhour} = $hourcount; } else { $byhour{$knownhour} = 1; } } foreach (keys %byhour) { print "$_ had $byhour{$_}<br>"; }
And we are aiming for10 had 2 15 had 21 14 had 4
0 had 0 1 had 0 2 had 0 3 had 0 .. skip.. 14 had 4 15 had 23 16 had 0 .. skip..
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: extracting by date into hour for counting
by dragonchild (Archbishop) on Feb 03, 2005 at 18:57 UTC | |
by sulfericacid (Deacon) on Feb 03, 2005 at 19:23 UTC | |
|
Re: extracting by date into hour for counting
by trammell (Priest) on Feb 03, 2005 at 19:19 UTC | |
|
Re: extracting by date into hour for counting
by Ardemus (Beadle) on Feb 03, 2005 at 20:16 UTC |