Jump to content

What Is Wrong With My Cgi Script?


bgmenot
 Share

Recommended Posts

I'm attempting to write a counting script, to see how many times a college has been clicked on. Right now my issue is that it counts only for the first college in my array. I have links below to text for both the html file and cgi script.

 

I understand I have to do something about a string and whatever, but not enough to know what I'm doing. If possible, could you post the code to fix it as well?

 

 

 

cgi script--->

http://docs.google.com/Doc?id=dd8mm9ph_7...

 

html document--->

http://docs.google.com/Doc?id=dd8mm9ph_7...

Link to comment
Share on other sites

im not a Perl guru, but I can tell you that this doesn't look right:

 

my @college_count = (0, 0, 0, 0, 0, 0, 0, 0);

...

foreach my $rec (@records) {

chomp($rec);

($college, $mascot) = split(/,/, $rec);

$college_count[$college] = $college_count[$college] + 1;

}

 

@college_count is regular array, but you are trying to use it as hash array. $college in your loop is a string, and $college_count[#] need an index - a number. Perl evaluates you string to 0, thus, you're always increasing $college_count[0].

 

firstly, read up on perl hash arrays.

 

you could try something like

 

>my %college_count = (
	"Baker College" => 0,
	...
	"University of Michigan" => 0
);


# then in the loop
$college_count{$college}++;

#and to print out all colleges with counts
while ( my ($col, $count) = each(%college_count) )
{
	print "$col = $count\n";
}

 

 

good luck

Edited by flamey
Link to comment
Share on other sites

Ok that kindof works, but not quite. If I do that, whichever college I click on will receive all of the click "points." For example if I click on Lawrence Tech, it will show 40 clicks and the rest with zero. If I click Baker next time, it will have 41 clicks, with the rest at zero. Any more help on fixing this would be appreciated.

Link to comment
Share on other sites

try this. add whatever you're missing, wrap with html if that's what you're looking for..

 

>use strict;
use warnings;

#declare variables
my ($college, $mascot, $size, @errors);
my %college_list = ("Baker College" => "the Ferret",
				"University of Michigan" => "the Wolverine",
				"Michigan State University" => "the Spartans",
				"Eastern Michigan University" => "the Swoop Eagle",
				"Central Michigan University" => "the Flying C",
				"Wayne State University" => "W The Warrior",
				"Lawrence Tech University" => "the Blue Devil",
				"University of Detroit-Mercy" => "Tommy Titan");

# init college_counts to zeros
my %college_count;
foreach my $col (keys %college_list) {
$college_count{$col} = 0;
}


#assign ramdom input items to variables for test purpose
$college = (keys %college_list)[rand keys %college_list];
$mascot = $college_list{$college};


push(@errors, "Please make a valid college choice") if !exists $college_list{$college};

if (@errors)
{
print $errors[$_]."\n" for 0..$#errors;
}
else
{
# save form data to a file
open(OUTFILE, ">> survey.txt") or die "Error opening survey.txt. $!, stopped";
print OUTFILE "$college,$mascot\n";
close(OUTFILE);

# read stats
open(INFILE, "<survey.txt") or die "Error opening survey.txt. $!, stopped";
my @records = <INFILE>;
close(INFILE);

# calculate survey statistics
foreach my $rec (@records)
{
	chomp($rec);
	my ($c, $m) = split(/,/, $rec);
	$college_count{$c}++;
}

# print all counts
while ( my ($col, $count) = each(%college_count) ) {
	print "$col = $count\n";
}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...