in reply to Storing karma

How about (untested)
open X,'karma.txt';undef$/; my %x=split/\s+/,<X>; $x{$user}++; close X;open X,'>karma.txt'; print X "$_ $x{$_}\n" for keys %x; close X;

Replies are listed 'Best First'.
Re: Re: Storing karma
by Anonymous Monk on Jun 28, 2001 at 17:36 UTC
    Anonymous Monk seems to have the right idea: Just load the whole database into a hash, increment the user element (it will be created if non-existant), and save the whole thing overwriting the file.

    If the user database gets too large to be handy, an upgrade would be to tie the hash to a database of some kind. There is one that seems universal to perl installations. ( NDBM? )

    use NDBM_File; tie(%x, 'NDBM_File', 'karma.db', 1, 0); $x{$user}++; untie(%x);
    Here the hash keys and values are maintained in the database and some subset is also maintained in the hash itself, but elements are transferred implicitly from the database to the hash (and vice-versa) as needed. The 'untie' line puts all new and changed elements out to the database.

    That last paragraph is clear as mud, hmm.. This page may be of use (or not).

    Amused

    Edit by tye