use strict; use warnings; use List::Util qw(sum); my %data; while (<DATA>) { my ($time, $value) = (split)[2..3]; next unless $time =~ /:/; my $hour = (split /:/, $time)[0]; push @{ $data{$hour} }, $value; } for my $hour (sort keys %data) { my @vals = @{ $data{$hour} }; print "Hour=$hour, Count=", scalar @vals, " Avg=", sum(@vals)/@val +s, "\n"; } __DATA__ itemid FROM_UNIXTIME(clock) value 26603 2010-10-09 00:00:23 38784360.8136 26603 2010-10-09 00:01:23 36529742.6667 26603 2010-10-09 00:02:23 36966880.8000 26603 2010-10-09 00:03:23 35666405.8667 26603 2010-10-09 01:05:23 35600190.6667 26603 2010-10-09 02:06:23 39175029.2000 26603 2010-10-09 03:07:23 35386478.4000 26603 2010-10-09 04:08:27 35773482.7500 26603 2010-10-09 05:09:23 39073367.5714 26603 2010-10-09 06:10:23 34379049.8667 26603 2010-10-09 07:11:24 33984045.7705 26603 2010-10-09 09:12:23 36276301.8305 26603 2010-10-09 10:13:23 37248366.0000 up to time 23:59 26603 2010-10-09 23:59:23 39393421.3333

prints:

Hour=00, Count=4 Avg=36986847.53675 Hour=01, Count=1 Avg=35600190.6667 Hour=02, Count=1 Avg=39175029.2 Hour=03, Count=1 Avg=35386478.4 Hour=04, Count=1 Avg=35773482.75 Hour=05, Count=1 Avg=39073367.5714 Hour=06, Count=1 Avg=34379049.8667 Hour=07, Count=1 Avg=33984045.7705 Hour=09, Count=1 Avg=36276301.8305 Hour=10, Count=1 Avg=37248366 Hour=23, Count=1 Avg=39393421.3333

In reply to Re: Calculate hourly averages by toolic
in thread Calculate hourly averages by addyrocker

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.