List Info

Thread: new and confused




new and confused
country flaguser name
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&quot;;
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>&lt;TITLE>;Seminar Workshop
School</TITLE></HEAD&gt;n";
print "<BODY>n&quot;;
print "<H1><B>Current registration totals for each
workshop:</B>;</H1&gt;n";
print "<TABLE>n&quot;;
print "<TR><TD>Computer Maintenance</TD&gt;<TD>;$workshop_count[0]
</TD&gt;</TR&gt;n";
print "<TR><TD>Microsoft Office</TD><TD>$workshop_count[1]
</TD></TR>n&quot;;
print "<TR><TD>Unix Essentials</TD>;<TD>$workshop_count[2]
</TD></TR>n";
print "<TR><TD>CGI/Perl</TD&gt;<TD&gt;$workshop_count[3]</TD>;</TR>;n";
print "<TABLE>&lt;BR>n&quot;;
print "<p>Total Registered:$total_count{total}</p>n";
print "</BODY>&lt;/HTML>n";

Any Help would be greatly appreciated.....Thanks in advance!

__._,_.___
.

__,_._,___
Re: new and confused
country flaguser name
Czech Republic
1969-12-31 18:00:00

From: "sleepygal33&quot; < sleepdriver%40bellsouth.net">sleepdriverbellsouth.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
&gt; #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!

&gt; my %total_count = ("Computer Maintenance", 0,
> "Microsoft Office&quot;, 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")
&gt; or die "Error opening c05ex5.txt. $!, stopped&quot;;
> print OUTFILE "$name,$workshopn";
> close(OUTFILE);
&gt;
> #calculate number of registrations per each workshop
> open(INFILE, "< c05ex5.txt")
&gt; or die "Error opening c05case2.txt. $!, stopped&quot;;
> records = <INFILE>;
>; close(INFILE);
>; foreach my $rec (records) {
> chomp($rec);
> ($name, $workshop) = split(/,/, $rec);
&gt; $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>&lt;TITLE>;Seminar Workshop
> School</TITLE></HEAD&gt;n";
> print "<BODY>n&quot;;
>; ...

This is easier written using qq{} or here-doc:

print qq{<HTML><HEAD><TITLE>Seminar Workshop School</TITLE></HEAD&gt;
<BODY>
...
};

or

print <<&quot;*END*&quot;;
<;HTML>&lt;HEAD><TITLE&gt;Seminar Workshop School</TITLE></HEAD&gt;
<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&quot;;

here.

Jenda
===== Jenda%40Krynicky.cz">JendaKrynicky.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
country flaguser name
United States
1969-12-31 18:00:00

>>&gt;>> "sleepygal33&quot; == sleepygal33 < sleepdriver%40bellsouth.net">sleepdriverbellsouth.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&quot; 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">merlynstonehenge.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]

about | contact  Other archives ( Real Estate discussion Medical topics )