Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > storing a hash of a hash in a DBM database

Reply
Thread Tools

storing a hash of a hash in a DBM database

 
 
Colvin
Guest
Posts: n/a
 
      12-29-2003
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);

 
Reply With Quote
 
 
 
 
Ragnar Hafstaš
Guest
Posts: n/a
 
      12-29-2003
"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





 
Reply With Quote
 
 
 
 
pkent
Guest
Posts: n/a
 
      12-29-2003
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
 
Reply With Quote
 
Colvin
Guest
Posts: n/a
 
      12-30-2003
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);
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
Storing Object in DBM Hash michael.shnitzer@gmail.com Perl Misc 2 06-21-2007 12:56 PM
Store Object Class in DBM Hash michael.shnitzer@gmail.com Perl Misc 2 05-17-2007 09:20 PM
Three questions: UTF-8, DBM, hash of lists, ... Wes Groleau Perl Misc 6 01-16-2005 01:45 PM
Sorting entries in a DBM hash by one of the variables Topher Perl 1 01-21-2004 02:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57