Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: incrementing array variables in a text file?

by friar tux (Novice)
on Apr 30, 2011 at 23:00 UTC ( [id://902268]=note: print w/replies, xml ) Need Help??


in reply to Re: incrementing array variables in a text file?
in thread incrementing array variables in a text file?

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

Replies are listed 'Best First'.
Re^3: incrementing array variables in a text file?
by wind (Priest) on Apr 30, 2011 at 23:05 UTC

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

      i tried it with hashes I am probably not even using them right and I probably have to put $votes{name} for every candidate in the script by $vote{sam}

      #!/usr/bin/perl use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; $prez=param('prez'); $vice=param('vice'); $secretary=param('secretary'); $count = 1; 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; print "<a href=www.smccme.edu>main page</a>";

      it spat out a bunch of syntax errors , and since i don't understand the syntax used with hashes I don't really see the problems. I think I am going to stick with what I know because I need to re-write this whole script from memory during class for the final. even if I get the hashes to work I doubt I could remember it all and understand what it all does by monday.it is just frustrating to have one last thing to do on this script after two weeks of working on it and be so close I'm going to try to figure out why the text file is changing it self around thanks for the help.

        Have use strict; on makes it so that all your variables need to be declared with my. This is useful since it means that the compiler will catch simple spelling mistakes of your variable names or accidentally using the wrong type of variable as well.

        Unfortunately, you haven't declared any of your variables with my so that's why you got so many initial errors. I'd recommend adding those pragmas first and fix the reported errors before attempting to change the code further.

        I get your dilemma though. It really is too bad that they didn't teach you good perl coding practices from the very beginning. Would make this easier.

Re^3: incrementing array variables in a text file?
by poj (Abbot) on May 01, 2011 at 12:03 UTC
    Remove the "\n" when you read the records in
    while(<VOTE>){ chomp ; # add ($user[$count],$vote[$count])=split (/\|/); $count++ }
    poj

      I tried the chomp and it didn't seem to work i also tried chop, is there anything else I need to add beside the chomp; to make it remove the "\n"? </code>

        It won't work if the vote.txt file already has extra newlines, try starting with a blank vote.txt file.
        If it still doesn't work try this instead of chomp
        s/[\r\n]//g;
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://902268]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found