friar tux has asked for the wisdom of the Perl Monks concerning the following question:
hi, I am working on my final for my perl class and have almost finished. it is a voting script, it all works fine except for when it needs to increment the new votes that are in the text file. the candidate labeled $vote1 will increment with no problem but the remaining 8 candidates will only be written to as 1 if voted for and the vote will be written over instead of added to if another voter uses the script. I believe it is something to do with the while loop and I have tried a number of combination and the foreach command instead. there is a comment about the problem right above it and is located near the end of the script
#!/usr/bin/perl use CGI ':standard'; print header; $prez=param('prez'); $vice=param('vice'); $secretary=param('secretary'); $count = 1; print "PREZ " . $prez ."<br>"; print "vice " . $vice . "<br>"; print "secretary " . $secretary . "<HR>"; #this opens the voting text file and extracts the information open (VOTE, "<vote.txt"); while(<VOTE>){ ($user[$count],$vote[$count])=split (/\|/); $count++ } close VOTE; #the variables for prez,vice and secretary are compared to these names + and increment there count by one for each vote they recieve if ($prez eq 'sam'){ $vote[1]++; } if ($prez eq 'hosea'){ $vote[8]++; } if ($prez eq 'howard'){ $vote[6]++; } if($vice eq 'wendy'){ $vote[7]++; } if($vice eq 'ann'){ $vote[3]++; } if($vice eq 'george'){ $vote[2]++; } if($secretary eq 'martha'){ $vote[5]++; } if($secretary eq 'tom'){ $vote[4]++; } if($secretary eq 'david'){ $vote[9]++; } $count = 1; #this is suppose to be the while loop which runs $vote[1] to #vote[9], + but it only saves the data for sam because he is $vote[1] all others + will show as a count of one if voted for, but will not increment if +voted for again open VOTE, ">vote.txt"; while (10 > $count){ print VOTE $user[$count]."|".$vote[$count]; $count++; } close VOTE; #this displays the vote tally for each candidate print "sam has $vote[1] votes<br>"; print "hosea has $vote[8] votes<br>"; print "howard has $vote[6] votes<br>"; print "wendy has $vote[7] votes<br>"; print "ann has $vote[3] votes<br>"; print "george has $vote[2] votes<br>"; print "martha has $vote[5] votes<br>"; print "tom has $vote[4] votes<br>"; print "david has $vote[9] votes<br>"; print " @vote"; print "<a href=backhome.cgi>main page</a>";
there are two scripts before this one that verify a password and allow the user to vote that work fine it is just the retention of the votes in the text file that doesn't work, I also need a way to prevent them from using the back button to vote again. it isn't a requirement but I would like to do it right. thank you for your time
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: incrementing array variables in a text file?
by wind (Priest) on Apr 30, 2011 at 20:09 UTC | |
by friar tux (Novice) on Apr 30, 2011 at 23:00 UTC | |
by wind (Priest) on Apr 30, 2011 at 23:05 UTC | |
by friar tux (Novice) on Apr 30, 2011 at 23:34 UTC | |
by wind (Priest) on May 01, 2011 at 00:09 UTC | |
by poj (Abbot) on May 01, 2011 at 12:03 UTC | |
by friar tux (Novice) on May 01, 2011 at 17:11 UTC | |
by poj (Abbot) on May 01, 2011 at 20:05 UTC | |
|