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-->
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-->