Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:06:49+00:00 2026-05-28T05:06:49+00:00

I have a space delimited text file I am reading and am trying to

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:06:50+00:00Added an answer on May 28, 2026 at 5:06 am

    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";
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a space delimited input text file. I would like to delete columns
I have a space delimited text file, from which I need to extract individual
I'd like to use lex/yacc to read in a delimited text file. This would
I have text like followings: delimited by space tab1 tab2 tab3 tab4 tab5 or
i have a text file that holds a 2-dimensional matrix. it looks like the
I have a text file that contains a data dump from a database. This
The problem I have a tab delimited input file that looks like so: Variable
I have a set of data that looks like this 201206040210 -3461.00000000 -8134.00000000 -4514.00000000
I am trying to replace values in a large space-delimited text-file and could not
So I have this piece of code that prompts for space delimited input of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.