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..

In reply to extracting by date into hour for counting by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.