What Is Wrong With My Cgi Script?

admin

Administrator
Staff member
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. <br /><br />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?<br /><br /><br /><br />cgi script---><br /><a href="http://docs.google.com/Doc?id=dd8mm9ph_7.." target="_blank">http://docs.google.com/Doc?id=dd8mm9ph_7..</a>.<br /><br />html document---><br /><a href="http://docs.google.com/Doc?id=dd8mm9ph_7.." target="_blank">http://docs.google.com/Doc?id=dd8mm9ph_7..</a>.<!--content-->
Welcome to the forums bgmenot <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />Sorry, but neither of those two links work.<!--content-->
Welcome to the forum, bgmenot. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Oops I'm sorry. Here. Same problem with (hopefully) fixed links<br /><br />cgi script--><br /><a href="http://docs.google.com/Doc?id=dd8mm9ph_71fszn2zc2" target="_blank">http://docs.google.com/Doc?id=dd8mm9ph_71fszn2zc2</a><br /><br />html page--><br /><a href="http://docs.google.com/Doc?id=dd8mm9ph_72gmzh37gk" target="_blank">http://docs.google.com/Doc?id=dd8mm9ph_72gmzh37gk</a><!--content-->
im not a Perl guru, but I can tell you that this doesn't look right:<br /><br /><!--fonto:Courier New--><span style="font-family:Courier New"><!--/fonto--><br />my @college_count = (0, 0, 0, 0, 0, 0, 0, 0);<br />...<br />foreach my $rec (@records) {<br /> chomp($rec);<br /> ($college, $mascot) = split(/,/, $rec);<br /> <b>$college_count[$college] = $college_count[$college] + 1;</b><br />}<!--fontc--></span><!--/fontc--><br /><br />@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].<br /><br />firstly, read up on perl hash arrays.<br /><br />you could try something like<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->my %college_count = (<br />        "Baker College" => 0,<br />        ...<br />        "University of Michigan" => 0<br />    );<br /><br /><br /># then in the loop<br />$college_count{$college}++;<br /><br />#and to print out all colleges with counts<br />while ( my ($col, $count) = each(%college_count) )<br />{<br />        print "$col = $count\n";<br />}<!--c2--></div><!--ec2--><br /><br /><br />good luck<!--content-->
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.<!--content-->
try this. add whatever you're missing, wrap with html if that's what you're looking for..<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->use strict;<br />use warnings;<br /><br />#declare variables<br />my ($college, $mascot, $size, @errors);<br />my %college_list = ("Baker College" => "the Ferret",<br />                    "University of Michigan" => "the Wolverine",<br />                    "Michigan State University" => "the Spartans",<br />                    "Eastern Michigan University" => "the Swoop Eagle",<br />                    "Central Michigan University" => "the Flying C",<br />                    "Wayne State University" => "W The Warrior",<br />                    "Lawrence Tech University" => "the Blue Devil",<br />                    "University of Detroit-Mercy" => "Tommy Titan");<br /><br /># init college_counts to zeros<br />my %college_count;<br />foreach my $col (keys %college_list) {<br />    $college_count{$col} = 0;<br />}<br /><br /><br />#assign ramdom input items to variables for test purpose<br />$college = (keys %college_list)[rand keys %college_list];<br />$mascot = $college_list{$college};<br /><br /><br />push(@errors, "Please make a valid college choice") if !exists $college_list{$college};<br /><br />if (@errors)<br />{<br />    print $errors[$_]."\n" for 0..$#errors;<br />}<br />else<br />{<br />    # save form data to a file<br />    open(OUTFILE, ">> survey.txt") or die "Error opening survey.txt. $!, stopped";<br />    print OUTFILE "$college,$mascot\n";<br />    close(OUTFILE);<br /><br />    # read stats<br />    open(INFILE, "<survey.txt") or die "Error opening survey.txt. $!, stopped";<br />    my @records = <INFILE>;<br />    close(INFILE);<br />    <br />    # calculate survey statistics<br />    foreach my $rec (@records)<br />    {<br />        chomp($rec);<br />        my ($c, $m) = split(/,/, $rec);<br />        $college_count{$c}++;<br />    }<br />    <br />    # print all counts<br />    while ( my ($col, $count) = each(%college_count) ) {<br />        print "$col = $count\n";<br />    }<br />}<!--c2--></div><!--ec2--><!--content-->
 
Top