Question

I have a space delimited text file I am reading and am trying to add up data based on month, the data looks like this:

Mon Apr 04 08:00:00 MDT 2011 120.72 0.3 0.707 25.609 25.609

Mon Apr 04 07:45:00 MDT 2011 119.94 0.3 0.707 25.443 25.443

I'm trying to just add up monthly totals:

#!/usr/bin/perl 

use strict;
use warnings;
use diagnostics;
use vars;

my $line;
my @data;
my @months;

my ($day, $month, $date, $time, $gmt, $year, $volt, $amp, $pf, $watt, $voltamp, 
    $voltsum, $wattsum, $count, $months, $monthlytotal );

$voltsum = 0;
$wattsum = 0;

open(DATAFILE, "@ARGV") ||  die $!;

@months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );

            while (<DATAFILE>) {
            $line = $_;
            chomp $line;
            @data = split(/\s/,$line);
            $day = $data[0];
            $month = $data[1];
            $date = $data[2];
            $time = $data[3];
            $gmt = $data[4];
            $year = $data[5];
            $volt = $data[6];
            $amp = $data[7];
            $pf = $data[8];
            $watt = $data[9];
            $voltamp = $data[10];

I want to match the month, add my data up, and print the result once, but my flow control is wrong, any idea on how to do this correctly?

I want read each line, test which month it is, add all similar months together, and return the result.

    foreach $months(@months) { 
       if ( $months =~ $month  ) {
                  $voltsum += $voltamp;
                  $wattsum += $watt;
            print "$month $year $wattsum $voltsum\n";
    }
            elsif ( $months !~ $month ) {
            $voltsum = 0;
            $wattsum = 0;

}
}

}
close (DATAFILE);



#               print "Month  Year Watts     Vars\n" ;
#               print "--------------------------\n";
#               print " $months $month    $year $wattsum $voltsum\n\n";
Was it helpful?

Solution

You might benefit from using a module to parse your timestamp. However, a simple fix might be to do something like this:

my %sum;
while (<>) {  # instead of open on @ARGV, just use diamond operator
    my ($day, $month, $date, $time, $gmt, $year, $volt, $amp, 
        $pf, $watt, $voltamp) = split;
    $sum{"$year$month"}{'voltamp'} += $voltamp;
    $sum{"$year$month"}{'watt'} += $watt;
    ....
}

Not exactly waterproof, but it might suit your needs. Then you can simply extract the month data with

for my $month (keys %sum) {
    print "voltamp sum ($month): $sum{$month}{'voltamp'}\n";
    ....
}

OTHER TIPS

The following data structure might be more appropriate (perldoc perldsc)

use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

my %sums;
while (<DATA>) {
    my @tokens = split;
    $sums{$tokens[1]}{volt   } += $tokens[6];
    $sums{$tokens[1]}{amp    } += $tokens[7];
    $sums{$tokens[1]}{pf     } += $tokens[8];
    $sums{$tokens[1]}{watt   } += $tokens[9];
    $sums{$tokens[1]}{voltamp} += $tokens[10];
}

print Dumper(\%sums);

__DATA__
Mon Apr 04 08:00:00 MDT 2011 120.72 0.3 0.707 25.609 25.609
Mon Apr 04 07:45:00 MDT 2011 119.94 0.3 0.707 25.443 25.443

Prints out:

$VAR1 = {
          'Apr' => {
                     'amp' => '0.6',
                     'pf' => '1.414',
                     'volt' => '240.66',
                     'voltamp' => '51.052',
                     'watt' => '51.052'
                   }
        };

If you want to do it streamable - as your program looks like -, assuming your data is sorted, you should first introduce a variable to check the month, which consists of month+year. This is safer (Apr 2011 is not Apr 2012).

  • Initialize the "last checked month"
  • init sum where needed and sum up.
  • print when it's time to print

Might look like:

my $last_month;

print "Month  Year Watts     Vars\n";
print "--------------------------\n";
while (<DATAFILE>) {
    # ... snip
    $voltamp = $data[10];

    my $current_month = $month . ' ' . $year; # Apr 2011 != Apr 2012
    if (!defined $last_month || $current_month ne $last_month) {
        # month changed
        if (defined $last_month) {
            # print intermediate sum
            print "$last_month $wattsum $voltsum\n";
        }
        $last_month = $current_month;
        $voltsum = 0;
        $wattsum = 0;
    }
    $voltsum += $voltamp;
    $wattsum += $watt;
}
print "$last_month $wattsum $voltsum\n"; # print last sum

However, I would also prefer summing up in a hash like the recent 2 answers that came up while writing this one, so I skip my second solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top