View Full Version : array and counting distinctions

03-15-2010, 08:32 AM
Hello everyone,

I am a CGI student completely new to the subject. I have a project due in two days, and I've been struggling with it all week. Unfortunately, my instructor poorly teaches the subject, and I am out of resources but the online world. May you please help me?

I have an html page that contains a form. This form is simply a list of radio buttons of college names.

In CGI, I must return the college name to the user..in addition to the college mascot (which is not a value in the html form). I'm lost about how to set up this array?

Lastly, I must also return a counter that is counting how many times the user clicks on a college radio button. I don't know where to start. The array scenario is giving me the most difficult time. Do I use an index array or hash? How do I communicate the college name with the mascot together?

I would appreciate any help you may have to offer. Thank you for your time in reading.

03-15-2010, 08:38 AM
where are the details of the mascot etc stored?

If you need to count how many times a user clicks on a radio button, you will likely need to use javascript.

welcome; by the way.


03-16-2010, 02:31 AM
Thanks bazz,

I am attempting to store details of the mascot in a hash array. Considering that my radio button form only has the value of college. My hash array will store the college next to its mascot.

My CGI code currently looks something like this:
I would appreciate any advice or feedback.

#final.cgi - saves form data to a file, and creates a dynamic
#Web page that displays a message and survey statistics
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;

#declare variables
my ($college, $mascot, $size, @records, @errors);
my %college_count = ("Baker", 0,
"Michigan", 0,
"State", 0,
"Eastern", 0,
"Central", 0,
"Wayne", 0,
"Lawrence", 0,
"Mercy", 0);
my %mascot = ("Baker", "Bears",
"State", "Spartans",
"Eastern", "Eagles",
"Central", "Chippewas",
"Wayne", "Warriors",
"Lawrence", "Devils",
"Mercy", "Titans");

#assign input items to variables
$college = param('College');

#validate input data
if($college ne "Baker" and $college ne "Michigan" and $college ne "State" and $college ne "Eastern" and $college ne "Central" and $college ne "Wayne" and $college ne "Lawrence" and $college ne "Mercy"){
push(@errors, "Select a college");

#determine size of @errors array
$size = @errors;

#process input data or display error page
if($size == 0){
#process input data
#save form data to a file
open(OUTFILE, ">>", "survey.txt")
or die "Error opening survey.txt. #!, stopped";
print OUTFILE "$college\n";

#calculate survey statistics
open(INFILE, "<", "survey.txt")
or die "Error opening survey.txt. $!, stopped";
@records = <INFILE>;
foreach my $rec (@records) {
($college) = split(/,/, $rec);
$college_count{$college} = $college_count{$college} + 1;

#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Thank you for visiting the American College web site!</H2>\n";

print "<H2>You selected $college.</H2>\n";

foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy"){
print "$key is home of the $mascot{$key}\n";

print "<EM><B>See the current college results below:</EM></B>\n";
print "<TABLE>\n";
foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy") {
print "<TR><TD>$key</TD> <TD>$college_count{$key}</TD></TR>\n";
print "</TABLE>\n";
print "</BODY></HTML>\n";
else {
#display error page
print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>You did not make a valid selection. Please click the BACK button and your browser \n";
print "and complete the survey. Thank you! </H2><BR>\n";
for(my $x = 0; $x < $size; $x = $x +1){
print "$errors[$x]<BR>\n";
print "</BODY></HTML>\n";

03-16-2010, 02:54 AM
OK, this is a homework and whilst you are making a good effort (in my opinion), the help we can give is limited. That said; the help I can give anyway is limited but I can give a few tips.

1. when a web page is being viewed and the user checks radio buttons, the page must then be submitted as a form for a second (processing) script to evaluate what was entered to that form. then you can see what college they chose and what the relveant mascot is.

If I understand your requirement that you need to know how many times the user clicked a raido button, eg he clicked button 1 three times, you'll need the client side scripting to work that out - javascript.

Because you have already worked out how to use arrays, my tip would be that the hash %college_count may be better as an @array which you can grep through instead of having this line..

if($college ne "Baker" and $college ne "Michigan" and $college ne "State" and $college ne "Eastern" and $college ne "Central" and $college ne "Wayne" and $college ne "Lawrence" and $college ne "Mercy"){
push(@errors, "Select a college");

you could have this (pseudo code)

unless ( grepping here ) {
push( @errors, 'message' );

Thne you can use that @array in your foreach loop

foreach my $var (@array){
process the output here [erhaps with a condition to check a match of the $var in the $hash{$var};

Besides that, I would suggest you read up on erm, something. can't recall the best title but basically, unless the value is a $variable, it should go in single quotes and if it is a variable, it should not have double quotes or nay quotes for that matter.



03-16-2010, 04:34 AM
You're using the wrong data structure. Instead of 2 separate hashes, you should use 1 HoH (hash-of-hashes).

my %college = (
Michigan => {
count => 0,
mascot => "Wolverines",


03-17-2010, 05:30 AM
Thank you all so much for your sharing of knowledge and help. After much debugging and more research, I was able to get my CGI working. I really appreciate all the help you have given to me. I learned so much, thank you.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum