|
List Info
Thread: new and confused
|
|
| new and confused |
  United States |
1969-12-31 18:00:00 |
|
Hi, I have been studying perl now for about three weeks and I really
am not understanding the variables regarding arrays and hash very
well. I have an assingment to write a script that adds up the total
amount of people registered for workshop seminars for a total of 4
seminars. Then to add up the total seminar registrations. I have
#!/usr/bin/perl
#c05ex5.cgi - saves form data to a file, and creates a dynamic
#Web page that displays the number of registrations per class
#and total registrations
print "Content-type: text/htmlnn";
use CGI qw(:standard -debug);
use strict;
#declare variables
my ($name, $workshop, $total, records);
my workshop_count = (0, 0, 0, 0, 0);
my %total_count = ("Computer Maintenance", 0,
"Microsoft Office", 0,
"Unix Essentials", 0,
"CGI/Perl", 0);
#assign input items to variables
$name = param('Name');
$workshop = param('Workshop');
#save form data to a file
open(OUTFILE, ">> c05ex5.txt")
or die "Error opening c05ex5.txt. $!, stopped";
print OUTFILE "$name,$workshopn";
close(OUTFILE);
#calculate number of registrations per each workshop
open(INFILE, "< c05ex5.txt")
or die "Error opening c05case2.txt. $!, stopped";
records = <INFILE>;
close(INFILE);
foreach my $rec ( records) {
chomp($rec);
($name, $workshop) = split(/,/, $rec);
$workshop_count[$workshop] = $workshop_count[$workshop] + 1;
$total_count{$total} = $total_count{$total} + 1;
}
#create Web page
print "<HTML><HEAD><TITLE>Seminar Workshop
School</TITLE></HEAD>n";
print "<BODY>n";
print "<H1><B>Current registration totals for each
workshop:</B></H1>n";
print "<TABLE>n";
print "<TR><TD>Computer Maintenance</TD><TD>$workshop_count[0]
</TD></TR>n";
print "<TR><TD>Microsoft Office</TD><TD>$workshop_count[1]
</TD></TR>n";
print "<TR><TD>Unix Essentials</TD><TD>$workshop_count[2]
</TD></TR>n";
print "<TR><TD>CGI/Perl</TD><TD>$workshop_count[3]</TD></TR>n";
print "<TABLE><BR>n";
print "<p>Total Registered:$total_count{total}</p>n";
print "</BODY></HTML>n";
Any Help would be greatly appreciated.....Thanks in advance!
__._,_.___
.
__,_._,___
|
| Re: new and confused |
  Czech Republic |
1969-12-31 18:00:00 |
|
From: "sleepygal33" < sleepdriver%40bellsouth.net">sleepdriver bellsouth.net>
> Hi, I have been studying perl now for about three weeks and I really
> am not understanding the variables regarding arrays and hash very
> well. I have an assingment to write a script that adds up the total
> amount of people registered for workshop seminars for a total of 4
> seminars. Then to add up the total seminar registrations. I have
>
> #!/usr/bin/perl
> #c05ex5.cgi - saves form data to a file, and creates a dynamic
> #Web page that displays the number of registrations per class
> #and total registrations
> print "Content-type: text/htmlnn";
> use CGI qw(:standard -debug);
> use strict;
>
> #declare variables
> my ($name, $workshop, $total, records);
> my workshop_count = (0, 0, 0, 0, 0);
It's generally better to declare variables only as you are about to
start using them instead of upfront. In this case you create a
variable $total, but never assign anything to it!
> my %total_count = ("Computer Maintenance", 0,
> "Microsoft Office", 0,
> "Unix Essentials", 0,
> "CGI/Perl", 0);
> #assign input items to variables
> $name = param('Name');
> $workshop = param('Workshop');
>
>
> #save form data to a file
> open(OUTFILE, ">> c05ex5.txt")
> or die "Error opening c05ex5.txt. $!, stopped";
> print OUTFILE "$name,$workshopn";
> close(OUTFILE);
>
> #calculate number of registrations per each workshop
> open(INFILE, "< c05ex5.txt")
> or die "Error opening c05case2.txt. $!, stopped";
> records = <INFILE>;
> close(INFILE);
> foreach my $rec ( records) {
> chomp($rec);
> ($name, $workshop) = split(/,/, $rec);
> $workshop_count[$workshop] = $workshop_count[$workshop] + 1;
> $total_count{$total} = $total_count{$total} + 1;
> }
1) It's not necessary to read the whole file into an array and then
loop through the array. Replace the
records = <INFILE>;
close(INFILE);
foreach my $rec ( records) {
by
while (defined(my $rec = <INFILE>)) {
In this case the file will probably be fairly small so it will not
make a big difference, but it would with bigger files.
2) Instead of
$variable = $variable + 1;
you can write
$variable += 1;
or
$variable++;
That is the
$workshop_count[$workshop] = $workshop_count[$workshop] + 1;
$total_count{$total} = $total_count{$total} + 1;
is better written
$workshop_count[$workshop]43;+;
$total_count{$total}++;
Also notice that you are using the $total variable without assigning
anything to it. I believe you meant $total_count{$name}. Or maybe
$total_count{$workshop}? I'm not sure what are the expected values of
the $name and $workshop. The way you use it, the $workshop should be
a number!
> #create Web page
> print "<HTML><HEAD><TITLE>Seminar Workshop
> School</TITLE></HEAD>n";
> print "<BODY>n";
> ...
This is easier written using qq{} or here-doc:
print qq{<HTML><HEAD><TITLE>Seminar Workshop School</TITLE></HEAD>
<BODY>
...
};
or
print <<"*END*";
<HTML><HEAD><TITLE>Seminar Workshop School</TITLE></HEAD>
<BODY>
...
*END*
> print "<p>Total Registered:$total_count{total}</p>n";
If you want to count the total number of registrations you don't need
any hash. A single scalar is enough. Looks to me like you wanted just
$total = $total + 1;
in the loop and
print "<p>Total Registered:$total</p>n";
here.
Jenda
===== Jenda%40Krynicky.cz">Jenda Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
__._,_.___
.
__,_._,___
|
| Re: new and confused |
  United States |
1969-12-31 18:00:00 |
|
>>>>> "sleepygal33" == sleepygal33 < sleepdriver%40bellsouth.net">sleepdriver bellsouth.net> writes:
sleepygal33> Hi, I have been studying perl now for about three weeks and I really
sleepygal33> am not understanding the variables regarding arrays and hash very
sleepygal33> well. I have an assingment to write a script that adds up the total
sleepygal33> amount of people registered for workshop seminars for a total of 4
sleepygal33> seminars.
"Assignment" implies that you are working in a school system. Please see your
teacher for help and other resources. Asking here is cheating, because you're
going outside for help, rather than having your own skill be tested.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
< merlyn%40stonehenge.com">merlyn stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
__._,_.___
.
__,_._,___
|
[1-3]
|
|