Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Arrays of anonymous hashes in a DBM file?

by nurikochan (Initiate)
on Apr 08, 2001 at 07:39 UTC ( #70780=perlquestion: print w/replies, xml ) Need Help??

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

Hello. First Post to, and whatnot. Hope you can help me...

Okay, I have a little bit of a problem. I need to store a complex data structure in a DBM file. It consists of an array of annonymous hashes. Here's an example of the data structure:

@yes_it_must_be_a_list = ( { 'value1' => "dada", 'value2' => "yadayada", } { 'value1' => "another value", 'value3' => "Ha! A different value than from the last hash!", } )

Any way I can store this in a DBM file? I've tried rewriting it as a hash so I can use an MLDBM file, but it would require a total rewrite of a couple thousand line program, and my attempts totally broke the program.

Anyone know how I could do this?

Replies are listed 'Best First'.
Re: Arrays of anonymous hashes in a DBM file?
by repson (Chaplain) on Apr 08, 2001 at 07:58 UTC
    I don't know much about DBM and MLDBM files but I do know a few methods other methods for persistant data structures.
    • Data::Dumper - This module generates a string representation of your data structure like your one above which you can write to a text file and restore with require or eval.
    • Storable - This module serializes your data structure making a binary representation of it and includes functions for storing and retriving from files. It has the advantage over Data::Dumper in that its stored structures use much less disk space.
    • XML::Dumper - This module produces a XML representation of your stucture but since it uses elements such as <HASH> and <ARRAY> it doesn't make much sense from a non-perl perspective.
    • XML::Simple - This module produces a file like XML::Dumper but doesn't use perl conventions/identities/names.
    You may want to research these a bit more, but any of them will probably suit most needs with a minimum of hassle.
    There are probably more than I can't think of at the moment as well.
Re: Arrays of anonymous hashes in a DBM file?
by araqnid (Beadle) on Apr 08, 2001 at 22:07 UTC
    Perhaps you could look at the BerkeleyDB CPAN module.

    This is an interface to BerkeleyDB (2.x or 3.x) that uses the native API instead of the DB1.85 API used by DB_File.

    The reason I think it's relevant is that you can use a "Recno" format database, and tie it to an array. (However, the documenation is unclear on this, and I haven't got the module installed to hand).

    Here is an extract from an example in the documentation:

    use strict; use BerkeleyDB; my $filename = "text" ; unlink $filename ; my @h ; tie @h, 'BerkeleyDB::Recno', -filename => $filename, -Flags => DB_CREATE, -Property => DB_RENUMBER or die "Cannot open $filename: $!\n" ; # Add a few key/value pairs to the file $h[0] = "orange" ; $h[1] = "blue" ; $h[2] = "yellow" ; push @h, "green", "black" ;

    The problem then becomes if you can actually use MLDBM to tie an array instead of a hash, and TBH I don't know.

    But I would have thought it would be fairly easy to add such functionality to MLDBM if it isn't there.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://70780]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-04-22 03:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found