storing a hash of a hash in a DBM database

Discussion in 'Perl Misc' started by Colvin, Dec 29, 2003.

  1. Colvin

    Colvin Guest

    I'm trying to store a hash of a hash in a DBM database, but I keep
    running into problems with my references. The enclosed script runs
    without errors; the errors don't appear until I uncomment the commented
    lines (the DBM functionality). How do I fix my references to make them
    compatible with a DBM database?

    Thank you,
    Alec Colvin


    ----- error message ----------

    13457 Can't use string ("HASH(0xba0c34)") as a HASH ref while "strict
    refs" in use at c:\perl_test\database1.pl line 20.

    ----- code -------------------

    use strict;

    my %data;
    # dbmopen(%data, "c:\\perl_test\\database", 0666) or
    # die "Can't open the database";

    my $person1 = { "fname" => "Stevie",
    "lname" => "Wonder"
    };

    %data = ( "13457" => $person1,
    );

    my @ID_number = keys(%data);
    @ID_number = sort(@ID_number);

    my $i;
    for ($i = 0; $i < @ID_number; $i++) {
    print STDOUT "$ID_number[$i] ";
    print STDOUT qq|$data{$ID_number[$i]}->{"fname"} |;
    print STDOUT qq|$data{$ID_number[$i]}->{"lname"}\n|;
    }

    # dbmclose(%data);
    Colvin, Dec 29, 2003
    #1
    1. Advertising

  2. "Colvin" <> wrote in message
    news:...
    > I'm trying to store a hash of a hash in a DBM database, but I keep
    > running into problems with my references. The enclosed script runs
    > without errors; the errors don't appear until I uncomment the commented
    > lines (the DBM functionality). How do I fix my references to make them
    > compatible with a DBM database?


    I assume you mean that you want the values of your tied hash to be
    hashreferences.

    in that case you might want to look at the module MLDBM (Multi-Level DBM)

    gnari
    Ragnar Hafstað, Dec 29, 2003
    #2
    1. Advertising

  3. Colvin

    pkent Guest

    In article <>, Colvin <>
    wrote:

    > I'm trying to store a hash of a hash in a DBM database, but I keep
    > running into problems with my references.

    <snip>

    The easiest thing to do is use MLDBM (in my opinion):

    http://search.cpan.org/~chamas/MLDBM-2.01/lib/MLDBM.pm

    It say:
    "...store arbitrary perl data, including nested references. Thus, this
    module can be used for storing references and other arbitrary data
    within DBM databases."

    But note the WARNINGS section, particularly whether there's a record
    length limit in whichever DBM implementation you're using.

    P

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
    pkent, Dec 29, 2003
    #3
  4. Colvin

    Colvin Guest

    Your advice was very useful. I've included a modified version of the
    original script that now runs properly.

    Thank you,
    Alec Colvin

    ---------- code --------------

    use strict;
    use MLDBM 'DB_File';
    use DB_File;

    my %personnel;
    tie(%personnel, 'MLDBM', 'testmldbm', O_CREAT|O_RDWR,
    0640) or die "Couldn't tie the database";

    my $person1 = { "fname" => "Stevie",
    "lname" => "Wonder"
    };

    my $person2 = { "fname" => "Walter",
    "lname" => "Cronkite"
    };

    %personnel = ( "13457" => $person1,
    "23567" => $person2
    );

    my @ID_number = keys(%personnel);
    my $i;
    for ($i = 0; $i < @ID_number; $i++) {
    print STDOUT "$ID_number[$i] ";
    print STDOUT qq|$personnel{$ID_number[$i]}->{"fname"} |;
    print STDOUT qq|$personnel{$ID_number[$i]}->{"lname"}\n|;
    }

    untie(%personnel);


    Colvin wrote:
    > I'm trying to store a hash of a hash in a DBM database, but I keep
    > running into problems with my references. The enclosed script runs
    > without errors; the errors don't appear until I uncomment the commented
    > lines (the DBM functionality). How do I fix my references to make them
    > compatible with a DBM database?
    >
    > Thank you,
    > Alec Colvin
    >
    >
    > ----- error message ----------
    >
    > 13457 Can't use string ("HASH(0xba0c34)") as a HASH ref while "strict
    > refs" in use at c:\perl_test\database1.pl line 20.
    >
    > ----- code -------------------
    >
    > use strict;
    >
    > my %data;
    > # dbmopen(%data, "c:\\perl_test\\database", 0666) or
    > # die "Can't open the database";
    >
    > my $person1 = { "fname" => "Stevie",
    > "lname" => "Wonder"
    > };
    >
    > %data = ( "13457" => $person1,
    > );
    >
    > my @ID_number = keys(%data);
    > @ID_number = sort(@ID_number);
    >
    > my $i;
    > for ($i = 0; $i < @ID_number; $i++) {
    > print STDOUT "$ID_number[$i] ";
    > print STDOUT qq|$data{$ID_number[$i]}->{"fname"} |;
    > print STDOUT qq|$data{$ID_number[$i]}->{"lname"}\n|;
    > }
    >
    > # dbmclose(%data);
    >
    Colvin, Dec 30, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Topher
    Replies:
    1
    Views:
    1,123
    Gunnar Hjalmarsson
    Jan 21, 2004
  2. Jonathan Wood
    Replies:
    1
    Views:
    507
    Jonathan Wood
    Jun 2, 2008
  3. Wes Groleau
    Replies:
    6
    Views:
    133
    Tad McClellan
    Jan 16, 2005
  4. Replies:
    2
    Views:
    140
    Martien verbruggen
    May 17, 2007
  5. Storing Object in DBM Hash

    , May 16, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    149
    -berlin.de
    Jun 21, 2007
Loading...

Share This Page