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

In January I was given effective advice about how to use the Storable Module
to store hashes in files for fast retrieval.
I now need to extend this so that I can store more than
one hash in each file.
I get the impression from various documents that this is possible.
However, I have not understood them well enough to do it nor have I been able to find any examples.
Therefore help from any knowing Monk will be appreciated.
  • Comment on Using Storable Module with Multiple Hashes

Replies are listed 'Best First'.
Re: Using Storable Module with Multiple Hashes
by dorward (Curate) on Jun 20, 2005 at 10:46 UTC

    I'd assume that you just use Storable to store an array of hash refs.

      Or a hash of hash refs, if you like to retrieve them by name later.

      In both cases, if you want to store hashes %a, %b, and %c with Storable, just store the compound structure instead:

      use Storable; store [ \%a, \%b, \%c] , 'file'; # or store { a => \%a, b => \%b, c => \%c} , 'file'; my $arrayref = retrieve('file'); my $a = $arrayref->[0]; # or my $hashref = retrieve('file'); my $a = $hashref->{a};
        That works well.
        Just in case anyone else needs to use this
        I think that the actual hashes are found by
        %aa = %{$hashref->{a}}; %bb = %{$hashref->{b}};
        Thanks to the Janaury helper monk
Re: Using Storable Module with Multiple Hashes
by tlm (Prior) on Jun 20, 2005 at 11:47 UTC

    TMTOWTDI.

    As dorward says, you can use an AoH:

    my @aoh = ( \%hash_a, \%hash_b, \%hash_c ); store \@aoh, 'AoH.sto'; # ... # elsewhere... my $aoh = retrieve 'AoH.sto' or die "failed to retrieve stored aoh"; my $hash_c = $aoh->[ 2 ];
    ...but you can also use a HoH:
    my %hoh = ( a => \%hash_a, b => \%hash_b, c => \%hash_c ); store \%hoh, 'HoH.sto'; # ... # elsewhere... my $hoh = retrieve 'HoH.sto' or die "failed to retrieve stored hoh"; my $hash_c = $hoh->{ c };

    the lowliest monk