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

Just read a few tutorials on SDBM and got my first database running! I currently know how to store things in the db using $dbm{'notverified'} = $form{'usermail'}; , but that only stores the key/value notverified: usermail. My question is how would I combine more than one value to the key? I'm aiming for something like $dbm{'notverified'} = "$form{'usermail'}", "$ID"; so it would store something like:
$notverified: test@admin.com 1xtrsnf
Thanks for any and all help!

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: DBM keys/values
by robartes (Priest) on Feb 12, 2003 at 08:18 UTC
    Well, the basic principle of hashes is that there's one value to one key, so you have to cheat. In this case, just store a stringified list (e.g. using join) as the value. Untested example code:
    my @unverified_emails=('test@admin.com', '1xtrsnf'); $dbm{'notverified'}=join ",",@unverified_emails; foreach my $mail (split /,/, $dbm{'notverified'}) { print "$mail is not verified!\n"; }
    Joining with comma's is probably best, as I've never encountered an email address with one in it. That said, it is not actually illegal to include comma's in email addresses (as long as you escape them with backslashes, see RFC819), so there is a very small possibility you might need another string to delimit the addresses.

    Update:One way to take into account backslashed commas is a negative zero width lookbehind assertion in the split regexp: /(?<!\\),/.

    CU
    Robartes-

      This is my first database and in reality one of the first times of having to play with hashes. I didn't realise you couldn't store more than one value per key in a more basic way, it seems to me there would be a lot of use for it if it were built in so you wouldn't need to join/split.

      If your example is implemented would there be a way to choose which value to pull back or would you be stuck pulling both values for @unverified_emails?

      Thanks for your help!



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        Well, the restriction here is that you're storing stuff in an underlying DBM database, so you're stuck with one value per key. That's why you need to do join/split or whatever tricks.

        Otherwise, one would use a hash of hashes to store this information (as it's easier to search for stuff in a hash, so it makes sense to store the email addresses themselves in hashes as well). You could store something like this in DBM, but they you would be looking at bronto's suggestion of using serialisation techniques to flatten the data structure before storing it.

        CU
        Robartes-

Re: DBM keys/values
by bronto (Priest) on Feb 12, 2003 at 11:10 UTC

    You could serialize your values with one of the CPAN modules for the task, like Storable or FreezeThaw; or use a multilevel DBM astraction like MLDBM. Anyway if your needs are basic you could rather prefer to join different values with a control character, like \cR:

    $dbm{'notverified'} = join "\cR","$form{'usermail'}", "$ID" ;

    I chose CTRL-R as a mnemonic for Record separator.

    Ciao!
    --bronto


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
      Thanks for your suggestion Bronto, I don't really want to go use other DBMs or any multi-level or more advanced databases seeming I'm still trying to learn DBM :)

      Thanks for your help!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid