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

Storable.pm seems to be a great little module (for 1D anyways) , however I can't see to get multi-dimensional hashes to store properly. I only really need two dimensions i.e. $Hash{$Key1}{$Key2} Someone must have done this before. Some pointers and/or code snippets would be greatly appreciated. Here is my functional 1D code:
# Near start of function which Builds %WordsHash if ( -f 'data/BuildWordsHash.dat' ) { # Get a reference to the read in WordHashs $rhWordsHash = retrieve('data/BuildWordsHash.dat') or die"ERROR: can +'t retrieve WordsHash!\n"; print keys %$rhWordsHash; %::WordsHash=%$rhWordsHash; #Put the WordsHash back into the right + hash. undef($rhWordsHash); print "Using data/BuildWordsHash.dat; Delete and rerun to rebuild Wo +rdsHash.\n"; return; } #... # Near end of function which build %WordsHash # Save a copy of this WordsHash for later use eval { store(\%::WordsHash, 'data/BuildWordsHash.dat'); }; print "Error writing to file: $@" if $@; return 0;

Replies are listed 'Best First'.
Re: Storable.pm: Storing Multi-dimensional Hashes How?
by davorg (Chancellor) on Aug 04, 2000 at 12:52 UTC

    Personally I always use FreezeThaw for that kind of thing, but I believe they both work in a similar manner (you can also use Data::Dumper if you only want to use standard modules). FreezeThaw and Storable both have a freeze method which serialises a complex data structure and a thaw method which resurrects the original structure. You can therefore do something like this:

    use Storable; $serialized = freeze \%structure; # Write $serialised to a file. # Time passes # Read $serialised from a file %structure = %{thaw($serialized)};

    If you want to store complex hashes in a DBM file then check out MLDBM on CPAN. It hides all this from you and works with Storable, FreezeThaw and Data::Dumper.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
Re: Storable.pm: Storing Multi-dimensional Hashes How?
by mikfire (Deacon) on Aug 04, 2000 at 16:48 UTC
    As another method, Data::Dumper works quite well in my shop and we are regularly storing very complex data structures. It works something like this, where $donor is a reference to a complex structure
    sub Clone { my $donor = shift; my $file = shift; my ( $dna, $clone ); local $Data::Dumper::Indent = 0; # It will be eval'd, we don't n +eed pretty local $Data::Dumper::Purity = 1; # make sure all code is present local $Data::Dumper::Terse = 1; # makes the eval work well local $Data::Dumper::Deepcopy = 1; # tell Dumper our intent #--- # Interestingly enough, Dumper will embed the call to bless in the + DNA. # Which makes this whole analogy really quite accurate and somehow + strange #--- $dna = Dumper( $donor ); #--- # At this point, the data structure in $donor is stringified # into $dna. $dna can be written to file or whatever. # To get it back out, do something like what follows - # this is a cloning function, after all. #--- $clone = eval $dna; return $clone; }
    mikfire
Re: Storable.pm: Storing Multi-dimensional Hashes How?
by rhardy (Acolyte) on Aug 04, 2000 at 13:52 UTC
    Thanks for your response davorg. I'll take a look at MLDBM. I found something similar to your post using Supersearch. Wish I'd seen that before I posted, it didn't really help though. There really should be an option after a regular search. The code I found there doesn't work... I'm always getting something like this:
    Not a scalar string at blib\lib\Storable.pm (autosplit into blib\lib\a +uto/Storab le/thaw.al) line 238, at PoeCryptoLib.pm line 191
    I should know better than to code all night... ;o) It will make sense in the morning...
    Regards,
    RH
    "Happiness is understanding."