perlbrother,
I agree with BrowserUk regarding the non-use of a database for this. Don't get me wrong, databases are great but once you become experienced in perl, this is something you could have solved faster than it took you to write your post. I will walk you through my solution.

First, I considered how to handle the fact that you are interested in only 120 dates. One way to do this would be to provide the desired dates as an input parameter - preferably in a file. The input file becomes another parameter.

Next, I considered how to calculate the average itself. Assuming the values won't result in integer overflow, the simplest method would be to track the sum and total entries. The average is just the quotient of the two. Here is the finished program (not tested).

#!/usr/bin/perl use strict; use warnings; use Getopt::Std; my %opt; get_args(\%opt); my $desired = load_date_file($opt{d}); open(my $fh, '<', $opt{i}) or die "Unable to open '$opt{i}' for readin +g: $!"; while (<$fh>) { chomp; my ($id, $date, $val) = split ' ', $_, 3; next if ! defined $date || ! exists $desired->{$date}; $desired->{$date}{sum} += $val; $desired->{$date}{cnt}++; } for my $date (sort keys %desired) { my $sum = $desired->{$date}{sum} || 0; my $cnt = $desired->{$date} || 0; my $avg = $cnt ? sprintf('%.2f', $sum / $cnt) : 0; print join(',', $date, $sum, $cnt, $avg), "\n"; } sub load_date_file { my ($file) = @_; my %desired; open(my $fh, '<', $file) or die "Unable to open '$file' for readin +g: $!"; while (<$fh>) { chomp; if (! /^\d{4}$/) { warn "'$_' is not in MMYY format - skipping\n"; next; } $desired{$_} = undef; } return \%desired; } sub get_args { my ($opt) = @_; my $Usage = qq{Usage: $0 -d <date_file> -i <input_file> -h : This help message -d : The (d)ate file -i : The (i)nput file } . "\n"; getopts('hd:i:', $opt) or die $Usage; die $Usage if $opt->{h} || ! defined $opt->{d} || ! defined $opt-> +{i}; }

Cheers - L~R


In reply to Re: Calculating the average on a timeslice of data by Limbic~Region
in thread Calculating the average on a timeslice of data by perlbrother

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.