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
    Use a hash to store the votes.
    my %votes; my $file = 'vote.txt'; open my $ih, $file or die $! while (<$ih>) { chomp; my ($user, $count) = split ','; $votes{$user} = $count; } close $ih; $votes{sam}++; open my $oh, $file or die $! for my $user (keys %votes) { print $oh "$user,$votes{$user}\n"; } close $oh;

    On a side note, your code wasn't working because you weren't outputted a return for each record:

    # From print VOTE $user[$count]."|".$vote[$count]; # To print VOTE $user[$count]."|".$vote[$count]."\n";

      thanks for the help the "\n" was a good idea and I thought it was working but if you vote more then twice it starts moving the text file around and changing the votes.

      First vote

      | | |1 |1 | | | |1 |

      second vote

      | | |2 |2 | | | |2 |

      third vote

      | | |1 |1 |2 |2 | |1 |

      all three votes were for the same three people but it seems to start spreading them out. is there a certain way I need to setup the text file so it doesn't do this? I tried the hashes too but they didnt work for me probably because I never learned them and just don't understand the concept

        %hashes are unordered @array's where the keys are strings instead of integers. Learn them. They are the most important part of perl that you should be learning.

        I already showed you the code for reading and writing the votes in a %hash data structure to a file.

        Also, add use strict; and use warnings; to the beginning of your script, and since this is CGI, CGI::Carp as well.

        use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use strict; use warnings;
        Remove the "\n" when you read the records in
        while(<VOTE>){ chomp ; # add ($user[$count],$vote[$count])=split (/\|/); $count++ }
        poj