250 likes | 354 Views
Array and Hash Variables. Please use speaker notes for additional information!. <!cisopt.html> <html> <head><title>CIS Department</title></head> <body> <h1>CIS Department - options</h1> <form action="http://www.pgrocer.com/cgi-bin/begin/cisopt.cgi" method=post> <b><i>Name:</i></b>
E N D
Array and Hash Variables Please use speaker notes for additional information!
<!cisopt.html> <html> <head><title>CIS Department</title></head> <body> <h1>CIS Department - options</h1> <form action="http://www.pgrocer.com/cgi-bin/begin/cisopt.cgi" method=post> <b><i>Name:</i></b> <input type=text name=name size=25><br><br> <b><i>Degree Option:</i></b><br> <input type=radio name=option value=0>Programming Career<br> <input type=radio name=option value=1>Networking Career<br> <input type=radio name=option value=2>Webmaster Career<br> <input type=radio name=option value=3>Business Information Systems Career<br> <input type=radio name=option value=4>Multimedia and the Internet Career<br> <input type=radio name=option value=5>Computer Science Transfer<br> <input type=radio name=option value=6>Information Systems Transfer<br> <br> <input type=submit value="SUBMIT"> <input type=reset value="RESET"> </form> </body> </html>
#!/usr/bin/perl #cisopt.cgi #courses required use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($stuname, $stuoption); my @courses = ("CIS17/CIS53/CIT20", "CIS53/CIT20", "CIS13/CIS44/CIT20", "CIS11orCIS22/CIS13/CIT20", "CIS13/CIS44/CIT20", "CIS73", "CIS17orCISelective"); #assign input to variables $stuname = param('name'); $stuoption = param('option'); #web page print "<html><head><title>CIS Department Options</title><basefont size=5></head>\n"; print "<body>\n"; print "Student, $stuname, should take the following courses: \n"; print "$courses[$stuoption].\n"; print "</body></html>\n"; The element that will be retrieved from the array when the option that is sent is 0. Note that the option that is sent is stored as $stuoption. The element that will be retrieved if the option that is sent is 1. The element that will be retrieved if the option sent is 6. I am now going to the @courses array and using the scalar name $courses since I am only retrieving one element. The element I retrieve is the one that $stuoption is pointing to. Remember that $stuoption was assigned the value that was sent from the HTML document.
<!cisoptions.html> <html> <head><title>CIS Department</title></head> <body> <h1>CIS Department - options</h1> <form action="http://www.pgrocer.com/cgi-bin/begin/cisoptions.cgi" method=post> <b><i>Name:</i></b> <input type=text name=name size=25><br><br> <b><i>Degree Option:</i></b><br> <input type=radio name=option value=P>Programming Career<br> <input type=radio name=option value=N>Networking Career<br> <input type=radio name=option value=W>Webmaster Career<br> <input type=radio name=option value=B>Business Information Systems Career<br> <input type=radio name=option value=M>Multimedia and the Internet Career<br> <input type=radio name=option value=S>Computer Science Transfer<br> <input type=radio name=option value=I>Information Systems Transfer<br> <br> <input type=submit value="SUBMIT"> <input type=reset value="RESET"> </form> </body> </html>
#!/usr/bin/perl #cisoptions.cgi #courses required use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($stuname, $stuoption); my %courses = ("P","CIS17/CIS53/CIT20", "N","CIS53/CIT20", "W","CIS13/CIS44/CIT20", "B","CIS11orCIS22/CIS13/CIT20", "M","CIS13/CIS44/CIT20", "S","CIS73", "I","CIS17orCISelective"); #assign input to variables $stuname = param('name'); $stuoption = param('option'); #web page print "<html><head><title>CIS Department Options</title><basefont size=5></head>\n"; print "<body>\n"; print "Student, $stuname, should take the following courses: \n"; print "$courses{$stuoption}.\n"; print "</body></html>\n"; Here I am taking the scalar $courses from the hash %courses. The one I am taking is the one that matches $stuoption. Remember that $stuoption contains the option that was sent over from the HTML code. Note that in this case I am enclosing the key name which is $stuoption in curly braces.
From cisoptions.cgi Note that the order is the same on the form and in the code. my %courses = ("P","CIS17/CIS53/CIT20", "N","CIS53/CIT20", "W","CIS13/CIS44/CIT20", "B","CIS11orCIS22/CIS13/CIT20", "M","CIS13/CIS44/CIT20", "S","CIS73", "I","CIS17orCISelective"); From cisoptions1.cgi Note that the order has been changed so it no longer matches the order on the form. my %courses = ("P","CIS17/CIS53/CIT20", "W","CIS13/CIS44/CIT20", "M","CIS13/CIS44/CIT20", "B","CIS11orCIS22/CIS13/CIT20", "S","CIS73", "N","CIS53/CIT20", "I","CIS17orCISelective");
#!/usr/bin/perl #cisoptions1.cgi #courses required use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($stuname, $stuoption); my %courses = ("P","CIS17/CIS53/CIT20", "W","CIS13/CIS44/CIT20", "M","CIS13/CIS44/CIT20", "B","CIS11orCIS22/CIS13/CIT20", "S","CIS73", "N","CIS53/CIT20", "I","CIS17orCISelective"); #assign input to variables $stuname = param('name'); $stuoption = param('option'); #web page print "<html><head><title>CIS Department Options</title><basefont size=5></head>\n"; print "<body>\n"; print "Student, $stuname, should take the following courses: \n"; print "$courses{$stuoption}.\n"; print "</body></html>\n"; Note the code to send fatal errors to the browser. I find this very helpful for debugging.
#!/usr/bin/perl #cisoptforeach.cgi #courses required use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my (@syscode, $key); my %courses = ("P","Programming = CIS17/CIS53/CIT20", "N","Networking = CIS53/CIT20", "W","Webmaster = CIS13/CIS44/CIT20", "M","Multimedia and Internet = CIS13/CIS44/CIT20", "B","Business Information Systems = CIS11orCIS22/CIS13/CIT20", "S","Computer Science = CIS73", "I","Information Systems = CIS17orCISelective"); #web page print "<html><head><title>CIS Department Options</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>CIS options and course suggestions for first semester</h1>\n"; @syscode = ("P","N","W","M","B","S","I"); foreach $key (@syscode) { print "$courses{$key}<br>\n"; } print "</body></html>\n"; The hash is made up of key, value, key, value. I have set up the array to match the letters in the hash. @syscode is an array of letter that match the keys in the hash. The for each will process each element in @syscode and use it as the key to access each course in %courses.
<!math.html> <html> <head><title>CIS Department</title></head> <body> <h1>Math Facts</h1> <form action="http://www.pgrocer.com/cgi-bin/begin/math.cgi" method=post> <b><i>Name:</i></b> <input type=text name=name size=25><br><br> <b><i>Math Facts:</i></b><br> Enter start number for math facts: <input type=text name=startnum size=5><br><br> Enter end number for math facts: <input type=text name=endnum size=5><br><br> <br> <input type=submit value="SUBMIT"> <input type=reset value="RESET"> </form> </body> </html>
#!/usr/bin/perl #math.cgi #courses required use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($stuname, $stustartnum, $stuendnum, $ans, $ct); #assign input to variables $stuname = param('name'); $ans = $stuname; $stustartnum = param('startnum'); $stuendnum = param('endnum'); #web page print "<html><head><title>MATH FACTS</title></head>\n"; print "<body>\n"; print "<h2>Math Facts</h2>\n"; for ($ct=$stustartnum; $ct<$stuendnum+1; $ct=$ct+1) { $ans=$ct + $ct; print "$ct + $ct = $ans<br>\n"; } print "</body></html>\n"; In my variable list I establish $ct to be the variable I use to control the for loop. $ans is established as the variable to hold the results of my calculation. The for statement sents $ct to the value of $stustartnum for the first pass through the loop. It then says that the termination of the loop is determined by whether $ct is still less than the $stuendnum +1. Each pass through the loop, $ct is incremented by 1. Inside the loop, I add $ct + $ct and store the answer as $ans. Then I print the math fact.
<!math1.html> <html> <head><title>CIS Department</title></head> <body> <h1>Math Facts</h1> <form action="http://www.pgrocer.com/cgi-bin/begin/math1.cgi" method=post> <b><i>Name:</i></b> <input type=text name=name size=25><br><br> <b><i>Math Facts:</i></b><br> Enter start number for math facts: <input type=text name=startnum size=5><br><br> Enter end number for math facts <input type=text name=endnum size=5><br><br> <br> <input type=submit value="SUBMIT"> <input type=reset value="RESET"> </form> </body> </html>
#!/usr/bin/perl #math1.cgi #courses required use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($stuname, $stustartnum, $stuendnum, $ans, $ct, $cti); #assign input to variables $stuname = param('name'); $ans = $stuname; $stustartnum = param('startnum'); $stuendnum = param('endnum'); #web page print "<html><head><title>MATH FACTS</title></head>\n"; print "<body>\n"; print "<h2>Math Facts</h2>\n"; for ($ct=$stustartnum; $ct<$stuendnum+1; $ct=$ct+1) { for ($cti=$stustartnum; $cti<$stuendnum+1; $cti=$cti+1) { $ans=$ct + $cti; print "$ct + $cti = $ans<br>\n"; } } print "</body></html>\n"; I have now established $ct and $ct1 as variables. I use $ct in the outer loop and $ct1 in the inner loop. This code shows the outer loop in orange and the inner loop in green. Once the inner loop starts it continues until it reaches the end point and then it returns to the outer loop. The for is finished when the outer loop reaches its end point.
$ct 1 1 1 1 1 2 2 2 2 2 3 3 3 … 5 $ct1 1 2 3 4 5 1 2 3 4 5 1 2 3 … 5 for ($ct=$stustartnum; $ct<$stuendnum+1; $ct=$ct+1) { for ($cti=$stustartnum; $cti<$stuendnum+1; $cti=$cti+1) { $ans=$ct + $cti; print "$ct + $cti = $ans<br>\n"; } }
for ($ct=$stustartnum; $ct<$stuendnum+1; $ct=$ct+1) { for ($cti=$stustartnum; $cti<$stuendnum+1; $cti=$cti+1) { $ans=$ct + $cti; print "$ct + $cti = $ans<br>\n"; } } Assume: When the for loop starts, $stustartnum=1 so $ct=1, and $stuendnum = 5. for 1=1; 1<5+1; $ct=$ct+1) { for 1=1; 1<5+1; $cti=$cti+1) { $ans=$ct + $cti; print "$ct + $cti = $ans<br>\n"; } } $ct$ct1 1 1 $ans is calculated as 1 + 1 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 1 2 Test and find that $ct1 is still less than $stuendnum +1 (5+1) so inner loop continues.
$ct$ct1 1 2 $ans is calculated as 1 + 2 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 1 3 Test and find that $ct1 is still less than $stuendnum +1 (5+1) so inner loop continues. $ct$ct1 1 3 $ans is calculated as 1 + 3 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 1 4 Test and find that $ct1 is still less than $stuendnum +1 (5+1) so inner loop continues.
$ct$ct1 1 4 $ans is calculated as 1 + 4 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 1 5 Test and find that $ct1 is still less than $stuendnum +1 (5+1) so inner loop continues. $ct$ct1 1 5 $ans is calculated as 1 + 5 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 1 6 Test and find that $ct1 is NOT less than $stuendnum +1 (5+1) so inner loop ends and control returns to outer loop.
The return to the outer loop means that $ct is incremented by 1 to make it 2 and then control drops to the inner loop. Because it is dropping down to start the inner loop new, $ct1 is reset to 1. $ct$ct1 2 1 $ans is calculated as 2 + 1 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 2 2 Test and find that $ct1 is still less than $stuendnum +1 (5+1) so inner loop continues. $ct$ct1 2 2 $ans is calculated as 2 + 2 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 2 3 Test and find that $ct1 is still less than $stuendnum +1 (5+1) so inner loop continues.
Assume that we now are at 5 and 5. $ct$ct1 5 5 $ans is calculated as 5 + 5 and the answer is printed. Returns to inner loop and increment $ct1 by 1. 5 6 Test and find that $ct1 is NOT less than $stuendnum +1 (5+1) so inner loop ends and control returns to outer loop. 6 6 Test and find that $ct is NOT less than $stuendnum + 1 (5+1) so outer loop ends and processing stops.