Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to keep running total?

by zBernie (Novice)
on Apr 27, 2013 at 14:26 UTC ( [id://1030968]=perlquestion: print w/replies, xml ) Need Help??

zBernie has asked for the wisdom of the Perl Monks concerning the following question:

The script below analyzes a fax log file, and shows the number of entries sent on a given day, and also the number of pages sent on that day. It increments a counter by 1 ($faxctr) and keeps a running total of the number of pages sent each day ($pagectr). The number of faxes per day is accurate, but the pages are usually off by 1 or 2. I'm almost certain there must be a more perlish approach to this problem, perhaps incrementing elements of a hash. Any suggestions would be greatly appreciated. Here's the output of the script below:
: 1, Pages: 6 04/26: 9, Pages: 72 04/25: 9, Pages: 31 04/24: 17, Pages: 30 04/23: 14, Pages: 42 04/22: 21, Pages: 43 04/19: 17, Pages: 36
#!/usr/bin/perl use warnings 'all'; use strict; use diagnostics; use Data::Dumper; my $lastdate = ""; my $faxctr = 1; my $pagectr = 1; my $state = 1; my $ipagectr; my @lines = <DATA>; shift(@lines); foreach my $line (@lines) { $line =~ s/\"//g; my ($reqid, $date, $time, $pages, $pages_sent, $client, $phone) = sp +lit('\s+',$line); if ($state eq 1) { my $ipagectr = $pages; } if ($date ne $lastdate) { $pagectr += $pages; $pagectr += $pages + $ipagectr if ($ipagectr gt 0); print "$lastdate: $faxctr, Pages: $pagectr\n"; $faxctr = 1; $pagectr = 1; $ipagectr = 0; } else { $faxctr++; $pagectr += $pages; $state = 0; } $lastdate = $date; } __DATA__ Req-id -Submitted- npg nps Clientid Phone number Attmpt Result +att 8502 04/26 13:07 5 5 county 17323411409 NORMAL NORMAL + 1* 8501 04/26 12:28 3 3 custserv 8456341521 NORMAL NORMAL + 1* 8500 04/26 12:26 2 2 accounti 856-853-0146 NORMAL NORMAL + 1* 8499 04/26 12:00 1 1 county 16095186890 NORMAL NORMAL + 1* 8498 04/26 11:15 2 2 county 16092619636 NORMAL NORMAL + 1* 8497 04/26 11:04 1 1 county 16095186890 NORMAL NORMAL + 1* 8496 04/26 10:44 8 8 county 17323411409 NORMAL NORMAL + 1* 8495 04/26 10:41 50 50 county 17323411409 NORMAL NORMAL + 1* 8494 04/26 08:55 2 2 county 9736315339 NORMAL NORMAL + 1* 8493 04/25 17:38 2 2 county 19732391138 NORMAL NORMAL + 1* 8492 04/25 17:18 2 2 county 16093941699 NORMAL NORMAL + 1* 8491 04/25 15:59 2 2 county 18622428101 NORMAL NORMAL + 1* 8490 04/25 14:34 2 2 accounti 856-428-5092 NORMAL NORMAL + 1* 8489 04/25 14:27 14 14 county 16093941699 NORMAL NORMAL + 2* 8488 04/25 11:24 2 2 county 16095186890 NORMAL NORMAL + 2* 8487 04/25 11:14 2 2 county 16092619636 NORMAL NORMAL + 1* 8486 04/25 10:44 2 2 county 16092619636 NORMAL NORMAL + 1* 8485 04/25 09:02 2 2 county 16095186890 NORMAL NORMAL + 1* 8484 04/24 17:58 2 2 county 19732391138 NORMAL NORMAL + 1* 8483 04/24 16:01 1 1 vsifax 609-538-0145 NORMAL NORMAL + 1* 8482 04/24 15:55 2 2 vsifax 609-538-0145 NORMAL NORMAL + 1* 8481 04/24 15:47 2 2 county 16095186890 NORMAL NORMAL + 1* 8480 04/24 15:44 2 2 vsifax 609-538-0145 NORMAL NORMAL + 1* 8479 04/24 15:03 1 1 accounti 609-538-0145 NORMAL NORMAL + 1* 8478 04/24 14:23 2 2 county 17323411409 NORMAL NORMAL + 2* 8477 04/24 14:07 2 2 county 9736315339 NORMAL NORMAL + 1* 8476 04/24 13:19 1 1 county 16095186890 NORMAL NORMAL + 1* 8475 04/24 11:51 1 1 county 16095186890 NORMAL NORMAL + 1* 8474 04/24 10:42 2 2 county 9736315339 NORMAL NORMAL + 1* 8473 04/24 10:02 2 2 county 16095186890 NORMAL NORMAL + 2* 8472 04/24 09:33 2 2 njtax 717-735-0548 NORMAL NORMAL + 1* 8471 04/24 09:07 1 1 county 17325770328 NORMAL NORMAL + 1* 8470 04/24 08:36 2 2 county 16095186890 NORMAL NORMAL + 2* 8469 04/24 08:33 2 2 county 19084754905 NORMAL NORMAL + 1* 8468 04/24 08:26 2 2 county 19084754905 NORMAL NORMAL + 1* 8467 04/23 17:14 2 2 county 19732391138 NORMAL NORMAL + 1* 8466 04/23 14:24 8 8 county 9083530697 NORMAL NORMAL + 1* 8465 04/23 14:18 2 2 county 16092619636 NORMAL NORMAL + 1* 8464 04/23 13:23 2 2 county 16095186890 NORMAL NORMAL + 1* 8463 04/23 13:16 2 2 county 9736315339 NORMAL NORMAL + 1* 8462 04/23 13:14 1 1 county 16095186890 NORMAL NORMAL + 1* 8461 04/23 12:04 2 2 county 16092619636 NORMAL NORMAL + 1* 8460 04/23 11:44 2 2 county 16095186890 NORMAL NORMAL + 1* 8459 04/23 10:09 1 1 county 17325770328 NORMAL NORMAL + 1* 8458 04/23 09:59 3 3 county 19084754905 NORMAL NORMAL + 1* 8457 04/23 09:56 4 4 county 19084754905 NORMAL NORMAL + 1* 8456 04/23 09:08 2 2 county 16095186890 NORMAL NORMAL + 1* 8455 04/23 08:57 2 2 county 16095186890 NORMAL NORMAL + 1* 8454 04/23 08:56 2 2 county 16095186890 NORMAL NORMAL + 1* 8453 04/22 17:16 8 8 county 19732391138 NORMAL NORMAL + 1* 8452 04/22 15:54 2 2 county 17323411409 NORMAL NORMAL + 1* 8451 04/22 15:27 3 3 county 17323411409 NORMAL NORMAL + 1* 8450 04/22 15:04 3 3 county 17323411409 NORMAL NORMAL + 1* 8449 04/22 14:40 2 2 county 16092619636 NORMAL NORMAL + 1* 8448 04/22 13:12 2 2 county 16092619636 NORMAL NORMAL + 1* 8447 04/22 12:40 2 2 county 16095186890 NORMAL NORMAL + 1* 8446 04/22 12:28 2 2 county 16092619636 NORMAL NORMAL + 1* 8445 04/22 12:09 2 2 county 16093941699 NORMAL NORMAL + 1* 8444 04/22 12:02 2 2 county 16093941699 NORMAL NORMAL + 1* 8443 04/22 11:05 2 2 county 18622428101 NORMAL NORMAL + 1* 8442 04/22 10:57 2 2 county 16092619636 NORMAL NORMAL + 1* 8441 04/22 10:55 4 4 county 17323411409 NORMAL NORMAL + 1* 8440 04/22 10:49 1 1 county 16095186890 NORMAL NORMAL + 1* 8439 04/22 10:32 2 2 county 16095186890 NORMAL NORMAL + 1* 8438 04/22 10:07 2 2 county 16095186890 NORMAL NORMAL + 2* 8437 04/22 10:00 2 2 county 17323411409 NORMAL NORMAL + 1* 8436 04/22 09:39 1 1 county 16095186890 NORMAL NORMAL + 1* 8435 04/22 09:29 2 2 county 19084754905 NORMAL NORMAL + 1* 8434 04/22 09:19 1 1 county 16095186890 NORMAL NORMAL + 1* 8433 04/22 07:35 2 2 county 16092619636 NORMAL NORMAL + 4* 8432 04/19 16:51 1 1 county 16092619636 NORMAL NORMAL + 1* 8431 04/19 16:21 3 3 njtax 610- 497-4359 NORMAL NORMAL + 2* 8430 04/19 14:54 1 1 county 16095186890 NORMAL NORMAL + 1* 8429 04/19 14:16 2 2 county 16092619636 NORMAL NORMAL + 1* 8428 04/19 14:10 2 2 county 9736315339 NORMAL NORMAL + 1* 8427 04/19 14:04 1 1 county 17325770328 NORMAL NORMAL + 1* 8426 04/19 14:03 2 2 county 16093941699 NORMAL NORMAL + 1* 8425 04/19 13:40 1 1 county 16095186890 NORMAL NORMAL + 1* 8424 04/19 13:37 1 1 county 16095186890 NORMAL NORMAL + 1* 8423 04/19 13:37 3 3 accounti 908-756-1024 NORMAL NORMAL + 1* 8422 04/19 12:19 3 3 county 19084754905 NORMAL NORMAL + 1* 8421 04/19 12:04 1 1 county 16095186890 NORMAL NORMAL + 1* 8420 04/19 11:49 9 9 county 16095186890 NORMAL NORMAL + 2* 8419 04/19 10:16 1 1 county 16095186890 NORMAL NORMAL + 1* 8418 04/19 09:27 2 2 county 16095186890 NORMAL NORMAL + 1* 8417 04/19 09:13 1 1 county 16092619636 NORMAL NORMAL + 1* 8416 04/19 09:01 1 1 county 16095186890 NORMAL NORMAL + 1* 8415 04/18 16:05 1 1 county 19084754905 NORMAL NORMAL + 1* 8414 04/18 15:02 2 2 county 17323411409 NORMAL NORMAL + 1* 8413 04/18 14:24 19 19 county 19732391138 NORMAL NORMAL + 2* 8412 04/18 14:12 2 2 county 17323411409 NORMAL NORMAL + 1* 8411 04/18 12:46 5 5 county 19732391138 NORMAL NORMAL + 1* 8410 04/18 12:06 2 2 county 17323411409 NORMAL NORMAL + 1* 8409 04/18 11:19 2 2 county 17323411409 NORMAL NORMAL + 1* 8408 04/18 11:10 1 1 county 16095186890 NORMAL NORMAL + 1* 8407 04/18 10:54 3 3 county 16095186890 NORMAL NORMAL + 1* 8406 04/18 10:48 2 2 county 16095186890 NORMAL NORMAL + 1* 8405 04/18 09:56 2 2 county 17323411409 NORMAL NORMAL + 1* 8404 04/18 09:33 1 1 county 19084754905 NORMAL NORMAL + 1*

Replies are listed 'Best First'.
Re: How to keep running total?
by hdb (Monsignor) on Apr 27, 2013 at 14:49 UTC

    Using the date as key to a hash is key to the sums:

    use strict; use warnings; my %stats; <DATA>; # skipping headers while(<DATA>) { chomp; my ($reqid, $date, $time, $pages, $pages_sent, $client, $phone) = sp +lit /\s+/; $stats{$date}{nfaxes} ++; $stats{$date}{npages} += $pages; } print "Date\tFaxes\tPages\n"; for my $date (sort keys %stats) { print "$date:\t$stats{$date}{nfaxes}\t$stats{$date}{npages}\n"; } __DATA__ Req-id -Submitted- npg nps Clientid Phone number Attmpt Result +att 8502 04/26 13:07 5 5 county 17323411409 NORMAL NORMAL + 1* 8501 04/26 12:28 3 3 custserv 8456341521 NORMAL NORMAL + 1* 8500 04/26 12:26 2 2 accounti 856-853-0146 NORMAL NORMAL + 1* 8499 04/26 12:00 1 1 county 16095186890 NORMAL NORMAL + 1* 8498 04/26 11:15 2 2 county 16092619636 NORMAL NORMAL + 1* 8497 04/26 11:04 1 1 county 16095186890 NORMAL NORMAL + 1* 8496 04/26 10:44 8 8 county 17323411409 NORMAL NORMAL + 1* 8495 04/26 10:41 50 50 county 17323411409 NORMAL NORMAL + 1* 8494 04/26 08:55 2 2 county 9736315339 NORMAL NORMAL + 1* 8493 04/25 17:38 2 2 county 19732391138 NORMAL NORMAL + 1* 8492 04/25 17:18 2 2 county 16093941699 NORMAL NORMAL + 1* 8491 04/25 15:59 2 2 county 18622428101 NORMAL NORMAL + 1* 8490 04/25 14:34 2 2 accounti 856-428-5092 NORMAL NORMAL + 1* 8489 04/25 14:27 14 14 county 16093941699 NORMAL NORMAL + 2* 8488 04/25 11:24 2 2 county 16095186890 NORMAL NORMAL + 2* 8487 04/25 11:14 2 2 county 16092619636 NORMAL NORMAL + 1* 8486 04/25 10:44 2 2 county 16092619636 NORMAL NORMAL + 1* 8485 04/25 09:02 2 2 county 16095186890 NORMAL NORMAL + 1* 8484 04/24 17:58 2 2 county 19732391138 NORMAL NORMAL + 1* 8483 04/24 16:01 1 1 vsifax 609-538-0145 NORMAL NORMAL + 1* 8482 04/24 15:55 2 2 vsifax 609-538-0145 NORMAL NORMAL + 1* 8481 04/24 15:47 2 2 county 16095186890 NORMAL NORMAL + 1* 8480 04/24 15:44 2 2 vsifax 609-538-0145 NORMAL NORMAL + 1* 8479 04/24 15:03 1 1 accounti 609-538-0145 NORMAL NORMAL + 1* 8478 04/24 14:23 2 2 county 17323411409 NORMAL NORMAL + 2* 8477 04/24 14:07 2 2 county 9736315339 NORMAL NORMAL + 1* 8476 04/24 13:19 1 1 county 16095186890 NORMAL NORMAL + 1* 8475 04/24 11:51 1 1 county 16095186890 NORMAL NORMAL + 1* 8474 04/24 10:42 2 2 county 9736315339 NORMAL NORMAL + 1* 8473 04/24 10:02 2 2 county 16095186890 NORMAL NORMAL + 2* 8472 04/24 09:33 2 2 njtax 717-735-0548 NORMAL NORMAL + 1* 8471 04/24 09:07 1 1 county 17325770328 NORMAL NORMAL + 1* 8470 04/24 08:36 2 2 county 16095186890 NORMAL NORMAL + 2* 8469 04/24 08:33 2 2 county 19084754905 NORMAL NORMAL + 1* 8468 04/24 08:26 2 2 county 19084754905 NORMAL NORMAL + 1* 8467 04/23 17:14 2 2 county 19732391138 NORMAL NORMAL + 1* 8466 04/23 14:24 8 8 county 9083530697 NORMAL NORMAL + 1* 8465 04/23 14:18 2 2 county 16092619636 NORMAL NORMAL + 1* 8464 04/23 13:23 2 2 county 16095186890 NORMAL NORMAL + 1* 8463 04/23 13:16 2 2 county 9736315339 NORMAL NORMAL + 1* 8462 04/23 13:14 1 1 county 16095186890 NORMAL NORMAL + 1* 8461 04/23 12:04 2 2 county 16092619636 NORMAL NORMAL + 1* 8460 04/23 11:44 2 2 county 16095186890 NORMAL NORMAL + 1* 8459 04/23 10:09 1 1 county 17325770328 NORMAL NORMAL + 1* 8458 04/23 09:59 3 3 county 19084754905 NORMAL NORMAL + 1* 8457 04/23 09:56 4 4 county 19084754905 NORMAL NORMAL + 1* 8456 04/23 09:08 2 2 county 16095186890 NORMAL NORMAL + 1* 8455 04/23 08:57 2 2 county 16095186890 NORMAL NORMAL + 1* 8454 04/23 08:56 2 2 county 16095186890 NORMAL NORMAL + 1* 8453 04/22 17:16 8 8 county 19732391138 NORMAL NORMAL + 1* 8452 04/22 15:54 2 2 county 17323411409 NORMAL NORMAL + 1* 8451 04/22 15:27 3 3 county 17323411409 NORMAL NORMAL + 1* 8450 04/22 15:04 3 3 county 17323411409 NORMAL NORMAL + 1* 8449 04/22 14:40 2 2 county 16092619636 NORMAL NORMAL + 1* 8448 04/22 13:12 2 2 county 16092619636 NORMAL NORMAL + 1* 8447 04/22 12:40 2 2 county 16095186890 NORMAL NORMAL + 1* 8446 04/22 12:28 2 2 county 16092619636 NORMAL NORMAL + 1* 8445 04/22 12:09 2 2 county 16093941699 NORMAL NORMAL + 1* 8444 04/22 12:02 2 2 county 16093941699 NORMAL NORMAL + 1* 8443 04/22 11:05 2 2 county 18622428101 NORMAL NORMAL + 1* 8442 04/22 10:57 2 2 county 16092619636 NORMAL NORMAL + 1* 8441 04/22 10:55 4 4 county 17323411409 NORMAL NORMAL + 1* 8440 04/22 10:49 1 1 county 16095186890 NORMAL NORMAL + 1* 8439 04/22 10:32 2 2 county 16095186890 NORMAL NORMAL + 1* 8438 04/22 10:07 2 2 county 16095186890 NORMAL NORMAL + 2* 8437 04/22 10:00 2 2 county 17323411409 NORMAL NORMAL + 1* 8436 04/22 09:39 1 1 county 16095186890 NORMAL NORMAL + 1* 8435 04/22 09:29 2 2 county 19084754905 NORMAL NORMAL + 1* 8434 04/22 09:19 1 1 county 16095186890 NORMAL NORMAL + 1* 8433 04/22 07:35 2 2 county 16092619636 NORMAL NORMAL + 4* 8432 04/19 16:51 1 1 county 16092619636 NORMAL NORMAL + 1* 8431 04/19 16:21 3 3 njtax 610- 497-4359 NORMAL NORMAL + 2* 8430 04/19 14:54 1 1 county 16095186890 NORMAL NORMAL + 1* 8429 04/19 14:16 2 2 county 16092619636 NORMAL NORMAL + 1* 8428 04/19 14:10 2 2 county 9736315339 NORMAL NORMAL + 1* 8427 04/19 14:04 1 1 county 17325770328 NORMAL NORMAL + 1* 8426 04/19 14:03 2 2 county 16093941699 NORMAL NORMAL + 1* 8425 04/19 13:40 1 1 county 16095186890 NORMAL NORMAL + 1* 8424 04/19 13:37 1 1 county 16095186890 NORMAL NORMAL + 1* 8423 04/19 13:37 3 3 accounti 908-756-1024 NORMAL NORMAL + 1* 8422 04/19 12:19 3 3 county 19084754905 NORMAL NORMAL + 1* 8421 04/19 12:04 1 1 county 16095186890 NORMAL NORMAL + 1* 8420 04/19 11:49 9 9 county 16095186890 NORMAL NORMAL + 2* 8419 04/19 10:16 1 1 county 16095186890 NORMAL NORMAL + 1* 8418 04/19 09:27 2 2 county 16095186890 NORMAL NORMAL + 1* 8417 04/19 09:13 1 1 county 16092619636 NORMAL NORMAL + 1* 8416 04/19 09:01 1 1 county 16095186890 NORMAL NORMAL + 1* 8415 04/18 16:05 1 1 county 19084754905 NORMAL NORMAL + 1* 8414 04/18 15:02 2 2 county 17323411409 NORMAL NORMAL + 1* 8413 04/18 14:24 19 19 county 19732391138 NORMAL NORMAL + 2* 8412 04/18 14:12 2 2 county 17323411409 NORMAL NORMAL + 1* 8411 04/18 12:46 5 5 county 19732391138 NORMAL NORMAL + 1* 8410 04/18 12:06 2 2 county 17323411409 NORMAL NORMAL + 1* 8409 04/18 11:19 2 2 county 17323411409 NORMAL NORMAL + 1* 8408 04/18 11:10 1 1 county 16095186890 NORMAL NORMAL + 1* 8407 04/18 10:54 3 3 county 16095186890 NORMAL NORMAL + 1* 8406 04/18 10:48 2 2 county 16095186890 NORMAL NORMAL + 1* 8405 04/18 09:56 2 2 county 17323411409 NORMAL NORMAL + 1* 8404 04/18 09:33 1 1 county 19084754905 NORMAL NORMAL + 1*

    UPDATE: forgot to skip headers

      Now that's the perl way of doing things! -Thanks
Re: How to keep running total?
by jethro (Monsignor) on Apr 27, 2013 at 14:50 UTC

    First of all I don't see a reason to use a state machine. You just have lots of lines and a special action to do when the date changes. So I would throw out the variable $state completely

    Also there is no reason for the counting to be done in an else-clause. You want to count every line.

    To avoid printing the first time (when you haven't counted anything yet) just check for the empty string in date

    If you want to try it for yourself don't reveal the spoiler, otherwise here is how the loop could be done (untested):

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1030968]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-03-28 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found