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


In reply to incrementing array variables in a text file? by friar tux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.