Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Syntax Question

Sorry I'm new...

         

web_young

6:42 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



I'm trying to create a script that will display a page that shows how many people have signed up for particular seminars. I've got that part working but I also want to display a total number of people that have signed up.

Here's my script:

#!/usr/bin/perl
#seminar.cgi - creates a dynamic Web page that displays seminar signups
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;

#declare variables
my ($name, $seminar, @records);
my @seminar_count = (0, 0, 0);

#calculate survey statistics
open(INFILE, "<", "seminar.txt")
or die "Error opening seminar.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($name, $seminar) = split(/,/, $rec);
$seminar_count[$seminar] = $seminar_count[$seminar] + 1;
}

#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>Seminar Workshop</TITLE></HEAD>\n";
print "<BODY>\n";
print "<TABLE>\n";
print "<TR><TD><b>Seminar</b></TD> <TD><b>Registered</b></TD></TR>\n";
print "<TR><TD>Computer Maintenance</TD> <TD>$seminar_count[1]</TD></TR>\n";
print "<TR><TD>Microsoft Office</TD> <TD>$seminar_count[2]</TD></TR>\n";
print "<TR><TD>Unix Essentials</TD><TD>$seminar_count[3]</TD></TR>\n";
print "<TR><TD>CGI/Perl</TD><TD>$seminar_count[4]</TD></TR>\n";
print "</TABLE><BR>\n";
print "</BODY></HTML>\n";

Does anyone know what I need to add to get the total? Thanks in advance!

moltar

7:12 pm on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...

my $total;
foreach my $rec (@records) {
chomp($rec);
($name, $seminar) = split(/,/, $rec);
$seminar_count[$seminar] = $seminar_count[$seminar] + 1;
$total++;
}

...

Now $total contains total number of subscribers

moltar

7:19 pm on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In fact, I would rewrite the whole first part like this:

[perl]
#!/usr/bin/perl

#seminar.cgi - creates a dynamic Web page that displays seminar signups


use strict;


my @seminar_count;
my $total;
open(INFILE, '< seminar.txt') or die "Error opening seminar.txt. $!, stopped";
while (<INFILE>) {
chomp;
my ($name, $seminar) = split(/,/);
$seminar_count[$seminar]++;
$total++;
}
close(INFILE);

print "Content-type: text/html\n\n";
[/perl]

... the rest of the html output goes here

web_young

7:27 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



Thank you, that worked perfectly. I hope you all don't get sick of me asking questions while I'm learning this new language. :-)