Resin has asked for the wisdom of the Perl Monks concerning the following question:

My script is returning two variables... a name and a score. This works fine. However, I wanna append it to a simple text file that can be called as a hash in the future. This is what I have, but it isn't working:
%pickemdb = ($user => 'user', $score => 'score'); print PICKEMDB << %pickemdb;
Any help appreciated!

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

    In what format do you want your file to be?

    If the file doesn't have to be user-readable, I'd use Storable

    JSON provides a portable format that's rather user-readable but not very user-modifyable.

    There's also YAML and XML::Simple.

    Data::Dumper can be used to dump a structure as Perl code, so you can simple use do or eval, but that allows arbitrary execution of Perl code. I recommend against this.

    All of the above support the storage of complex structures. However, that makes them harder for users to edit them manually. A lot of modules on CPAN provide the facilities to read and write a variety of user-modifyable formats, but I don't have any to recommend.

    Why invent yet another format. There are plenty from which to choose already.

    Updated

      I should have been more clear in my question. The reason I don't go to pre-made modules is because this is part of an exercise I'm using to learn Perl and CGI. I'll use the add-on resources once I have a better idea of what I'm doing.
Re: Writing a hash of two variables to a file
by chargrill (Parson) on Oct 25, 2006 at 02:32 UTC

    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($,,$;,$*,$/)
      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

      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
Re: Writing a hash of two variables to a file
by bobf (Monsignor) on Oct 25, 2006 at 02:39 UTC
Re: Writing a hash of two variables to a file
by planetscape (Chancellor) on Oct 25, 2006 at 17:45 UTC