Fields won't add.

NevadaSam

New Member
Joined
Apr 28, 2006
Messages
9
Reaction score
0
Fields won't add.

In learning CGI/Perl I am trying to write a script that will read records and print the total number

of people registered for each seminar. This is the script I have been working on all day and the text

file which it reads follows it.

#!/usr/bin/perl
#list.cgi - reads form data to a file, and creates a dynamic
#Web page that displays people registering for different seminars.
print "Content-type: text/html\n\n";
use CGI qw:)standard -debug);
use strict;

#declare variables
my ($name, $seminar, @records, %sem_count);
my @name_count = (0);
my %sem_count = ("Computer Maintenance", 0,
"Microsoft Office", 0,
"Unix Essentials", 0,
"CGI/Perl", 0);

#calculate people registered and seminar count
open(INFILE, "<", "list.txt")
or die "Error opening list.txt. $!, stopped";
my @records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($name, $seminar) = split(/,/ , $rec);
$name_count [$name] = $name_count[$name] + 1;
$sem_count {$seminar} = $sem_count{$seminar} +1;
}

#generate HTML
print "<html><head><title>Seminar Workshop</title></head>\n";
print "<body>\n";
print "<table>\n";
print "<tr align='left'><th>Seminar</th> <th>Registered</th></tr>\n";
foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix Essentials", "CGI/Perl") {
print "<tr><td>$key</td> <td>$sem_count{$key}</td></tr>\n";
}
print "</table>\n";
print "<br /><br />Total registered: $name_count[$name]\n";
print "</body></html>\n";

Text file:
Janice Alto,3
Nancy Perez,1
James Houza,2
Beth Jimminez,2
Michael Hiller,4
Inez Smith,4
Paul Eniudo,3
Tess Bacza,2
Robert Hau,1
Opal Jones,2
The first field in the text file is a name which I have been able to add all

together to get a total of 10. What I haven't figured out how to do is total the second field to get

the number attending each seminar. The numbers represent which seminar by 1-Computer Maintenance,

2-Microsoft Office, 3-Unix Essentials, and 4-CGI/Perl. I keep coming up with 0 for each event.
 
Back
Top