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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



jffrank
New User

Apr 4, 2005, 12:48 PM

Post #1 of 3 (349 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:

#!/usr/bin/perl

$data_file="workpts.txt";
print "Content-type: text/html\n\n";
print "<HTML><BODY>";
print"<h3>CDR Worker Retention Points List</h3>";
print"<table>";
print"<tr><th>Name<th>Points<th>Date<th>Weekend<br>Chief<th>Details</th>";

open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

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

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


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


davorg
Thaumaturge / Moderator

Apr 5, 2005, 4:56 AM

Post #2 of 3 (343 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).


Code
#!/usr/bin/perl 

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
http://www.dave.org.uk/
Get more help at Perl Monks


jffrank
New User

Apr 5, 2005, 6:56 AM

Post #3 of 3 (341 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