Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This question may be a bit hard to follow and my code very sloppy but this is my best attempt at getting what I need.

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.

my @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>"; }
Live output
10 had 2 15 had 21 14 had 4
And we are aiming for
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
    You want an array and you want it pre-seeeded for the 24 values you're looking for.
    my @byhour = (0) x 24; foreach my $hour (@hours) { my ($knownhour) = $hour =~ /\s(\d\d)/; $byhour[$knownhour]++; } print "$_ had $byhour[$_]\n" for 0 .. $#byhour;

    Update: Fixed typo caused by not actually trying the code under strict.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I think you have a typo in this. You seem to be calling $hour from the array and $hours in the regex.

      This may be intentional but just something I noticed.



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: extracting by date into hour for counting
by trammell (Priest) on Feb 03, 2005 at 19:19 UTC
    How about this:
    for (0 .. 23) { my $count = $byhour{$_} || 0; print "$_ had $count\n"; }
Re: extracting by date into hour for counting
by Ardemus (Beadle) on Feb 03, 2005 at 20:16 UTC
    Your code is fine. Here it is slightly modified:
    use strict; my %byhour; my @hours = ( '2005-02-01 14:35:24','2005-02-01 10:35:24', '2005-02-01 14:35:24','2005-02-01 23:35:24', '2005-02-01 12:35:24'); foreach (@hours) # Use default variable $_ { /\s(\d\d)/; # Regex operates on $_ by default $byhour{$1}++; # Will create hash element if needed } foreach (0..23) { printf "%2d had %d\n", $_, $byhour{$_}}
    Ardemus - "This, right now, is our lives."