CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Sum of records in file?


New User

Apr 4, 2005, 12:48 PM

Post #1 of 3 (350 views)
Sum of records in file? Can't Post

I want to show the name and a total for each person listed in file, each person may have several records. The current report looks as follows. CDR Worker Retention Points List

Name Points Date Weekend
Chief Details Chris Frank 10 03/29/2005 Region Chief Hospitality John Frank 10 03/29/2005 Webmaster John Frank 10 03/30/2005

Region Chief T&s

The code is as follows:


print "Content-type: text/html\n\n";
print "<HTML><BODY>";
print"<h3>CDR Worker Retention Points List</h3>";

open(DAT, $data_file) || die("Could not open file!");

foreach $worker (sort @raw_data)
$fullname=sprintf("%s %s", $fname, $lname);
print "<tr><td>$fullname<td>$points<td>$dates<td>$chief<td>$detail";

print "</BODY></HTML>";

What changes are need to just list the name and total points?

Thaumaturge / Moderator

Apr 5, 2005, 4:56 AM

Post #2 of 3 (344 views)
Re: [jffrank] Sum of records in file? [In reply to] Can't Post

Something like this I guess (untested as I don't have an input file).


use strict;
use warnings;
use Fcntl ':flock';
use CGI ':standard';

my %people;
my $data_file="workpts.txt";
open DAT, $data_file or die "Could not open $data_file: $!\n";
flock DAT, LOCK_SH or die "Could not lock $data_file: $_\n";

while (<DAT>) {
my ($lname, $fname, $points) = split /\|/;

$people{"$fname $lname"} += $points;

print header;
print start_html;
print h1('CDR Worker Retention Points List');
my @header = qw(Name Points);
my $header = Tr(td(\@header));
my $body;
foreach (keys %people) {
$body .= Tr(td($_), td($people{$_}));

print table($header, $body);
print end_html;

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks

New User

Apr 5, 2005, 6:56 AM

Post #3 of 3 (342 views)
Re: [davorg] Sum of records in file? [In reply to] Can't Post

Thanks, Dave works great Smile


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives