Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   storing a hash of a hash in a DBM database (http://www.velocityreviews.com/forums/t884389-storing-a-hash-of-a-hash-in-a-dbm-database.html)

Colvin 12-29-2003 09:43 PM

storing a hash of a hash in a DBM database
 
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);


Ragnar Hafstaš 12-29-2003 10:05 PM

Re: storing a hash of a hash in a DBM database
 
"Colvin" <rdcolvin@erols.com> wrote in message
news:3FF09FE9.5060402@erols.com...
> 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






pkent 12-29-2003 10:59 PM

Re: storing a hash of a hash in a DBM database
 
In article <3FF09FE9.5060402@erols.com>, Colvin <rdcolvin@erols.com>
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

Colvin 12-30-2003 04:38 PM

Re: storing a hash of a hash in a DBM database
 
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);
>




All times are GMT. The time now is 02:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.