in reply to Re: Writing a hash of two variables to a file
in thread Writing a hash of two variables to a file

I just wanted to give the lines that were not working right.. here is the full part in case I'm doing something retarded:
open (PICKEMDB, '>>', "pickemdb.txt") or die "Can't write results."; $pickemdb{'user'} = $user; $pickemdb{'lastweek'} = $score; ###### Errr.. not sure if the above are part of the code or where just + a test that I commented out in case I might use it later. %pickemdb = ($user => 'user', $score => 'score'); print %pickemdb; print %pickemdb >> PICKEMDB; close (PICKEMDB);

Replies are listed 'Best First'.
Re^3: Writing a hash of two variables to a file
by ikegami (Patriarch) on Oct 25, 2006 at 04:30 UTC

    You can't just print the hash. You'll need to iterate through its keys, and print out each key and its associated value.

    Also, reread print for the proper syntax for writting to a file handle.

Re^3: Writing a hash of two variables to a file
by wfsp (Abbot) on Oct 25, 2006 at 09:06 UTC
    You want to do 2 things: 1) append some data to a text file and 2) load that file into a hash.

    Assuming that is correct you could try something like this to get you started:

    my $user = 'Resin'; my $score = 1000000; # append to db open (PICKEMDB, '>>', "pickemdb.txt") or die "Can't write results."; my $record = join '|', $user, $score; print PICKEMDB "$record\n"; close PICKEMDB; # later, read into hash open (PICKEMDB, '<', "pickemdb.txt") or die "Can't read results."; my %pickemdb; while (my $record = <PICKEMDB>){ chomp $record; my ($user, $result) = split /\|/, $record; $pickemdb{$user} = $result; } # traverse over the hash to see for my $user (keys %pickemdb){ print "$user -> $pickemdb{$user}\n"; }

    updated: changed second error message