in reply to Writing a hash of two variables to a file

First, you'll have to open your file. Note that if you want to append to it, you'll need to make use of '>>', but you may just wish to overwrite your file on successive writes ('>')

If you just try to print your %hash directly to the file, all your file will contain is something like "HASH(0x923483)" something not easily parsable (Thanks, ikegami). You might want to think of some way to loop through your hash keys and print out the data in a way that you can read back in later.

If you need more of a push in the right direction, let us know.

BTW, welcome to the monastery, Resin ... even though you've been around a while, I see that this is your first post to SoPW!



--chargrill
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Replies are listed 'Best First'.
Re^2: Writing a hash of two variables to a file
by Resin (Novice) on Oct 25, 2006 at 03:23 UTC
    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);

      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.

      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

Re^2: Writing a hash of two variables to a file
by ikegami (Patriarch) on Oct 25, 2006 at 02:44 UTC

    Printing the hash would give something like userJoescore97, not HASH(0x923483).

      I have a print "etc" on the line before writing to the file that looks like that on my test script. "6scoreResinuser" Maybe my < signs are backwards? And why is my rep going down? lol

        Most likely your rep is going down because you didn't specify the problem very well. To get good help you need a good question. Fuzzy questions generally get fuzzy answers as people try to guess what you may actually want.


        DWIM is Perl's answer to Gödel