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

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.

Replies are listed 'Best First'.
Re^5: incrementing array variables in a text file?
by wind (Priest) on May 01, 2011 at 00:09 UTC

    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.