StarRice
StarRice

Reputation: 23

(Perl) Trying to write a foreach statement with a simple array. Confused with the formatting

I'm a beginner in programming, this is my first language. And in my class we are using a slightly out of date book to learn with (Book copyrighted '02). Doubt this would affect you helping me much, but worth noting.

The problem

I don't know how to format a simple foreach statement using/combined with an array. I'm getting mixed up and my book doesn't provide examples. I'm trying to get it so the Uses/Primary_Uses are shown when the user checks multiple checkboxes.

#!/usr/bin/perl
#c04ex5.cgi - creates a dynamic Web page that acknowledges
#the receipt of a registration form
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;

#declare variables
my ($name, $serial, $modnum, $sysletter, $primary_uses, $use, @primary_uses, @uses);
my @models = ("Laser JX", "Laser PL", "ColorPrint XL");
my @systems = ("Windows", "Macintosh", "UNIX"); 
my @primary_uses = ("Home", "Business", "Educational", "Other");


#assign input items to variables
$name = param('Name');
$serial = param('Serial');
$modnum = param('Model');
$sysletter = param('System');
@primary_uses = param('Use');



#create Web page
print "<HTML><HEAD><TITLE>Juniper Printers</TITLE></HEAD>\n";
print "<BODY><H2>\n";
print "Thank you , $name, for completing \n";
print "the registration form.<BR><BR>\n";
print "We have registered your Juniper $models[$modnum] printer, \n";
print "serial number $serial.\n";
print "You indicated that the printer will be used on the\n";
print "$systems[$sysletter] system. <BR>\n";
print "The primary uses for this printer will be the following:\n";

#The part I'm having trouble with.     
foreach $use (@primary_uses) {
print "$use [@use]<BR>\n";
} 

print "</H2></BODY></HTML>\n";

My naming of variables might be a bit off, I was getting desperate and making sure I declare more than I should.

Upvotes: 2

Views: 121

Answers (2)

TLP
TLP

Reputation: 67900

If you wanted to print a simple list of items, you should just use the $use variable:

foreach $use (@primary_uses) {
    print "$use<BR>\n";
} 

Note that this will also remove the fatal error that comes from not declaring @use. Perhaps that was also a point of confusion for you. $use and @use are two completely different variables, despite having the same name.

Note that you can print a list with the CGI module very easily:

my $cgi = CGI->new;
print $cgi->li(\@primary_uses);

Outputs the list interpolated in a list html entity, like so:

<li>Home</li> <li>Business</li> <li>Educational</li> <li>Other</li>

Some other pointers:

Note that it is a good idea to declare your variables in the smallest scope possible

foreach my $use (@primary_uses) {   # note the use of "my"
    print "$use<BR>\n";
} 

That also goes with the other variables. A good idea is to declare them right as you initialize them:

my $name = param('Name');

Then people who read your code don't have to scan backwards in the file to see where the variable has "been" before.

Note that you should never, ever use the content of data from a web form without sanitizing it first, because it is a huge security risk, especially when you print it. It allows a web user to execute arbitrary code on your system.

You should know that for and foreach are aliases for the same function.

Also, you should always, always use warnings:

use warnings;

There really is no good reason to ever not turn warnings on.

Upvotes: 2

m79lkm
m79lkm

Reputation: 3070

foreach my $myuse (@primary_uses) {
    print $myuse;
}

You need to declare the variable.

Upvotes: 0

Related Questions