Operating System - HP-UX
1825782 Members
2150 Online
109687 Solutions
New Discussion

Re: Storing hashes of anonymous hashes

 
SOLVED
Go to solution
Mark Grant
Honored Contributor

Storing hashes of anonymous hashes

I have stuggled with storing hashes containing arrays and other hashes for some time now when using dbmopen() and always come up with some ugly solution that I don't like.

I want to store a hash containing an anonymous hash in a data file using "DB". This, I understand, isn't possible. However, to get around this, I have stored the code to create my "hash with anonymous hash" records as strings in another hash that is tied to the data file using the same key. I then use "eval" to set it all up.

To help explain what I am trying to achive, this code stores two hash records, each containing an anonymous hash.

%storage=();
%busses=();
use DB;

$busses{English}=( { "colour"=>"red","size"=>"big" });
$busses{Norwegian}=( { "colour"=>"green","size"=>"small" });

dbmopen %storage,"storage",0666;
foreach $i (keys %busses){
$storage{$i}="\$busses{$i}={\"colour\"=>\"$busses{$i}->{colour}\
",\"size\"=>\"$busses{$i}->{size}\"};";
}

Here is the code to retrieve a single record off of this data of the disk.

use DB;

%busses=();
%storage=();
dbmopen %storage ,"storage",0666;

$LOOKUP="Norwegian";

eval " $storage{$LOOKUP} ";

print "$LOOKUP busses are $busses{$LOOKUP}->{colour}\n";

Surprisingly, this seems to work.

My question is, is there a better, more sensible, less silly looking way of achieving this and if not, can you see any pitfalls with this approach apart fromt he waste of disk space.

Any comments appreciated.



Never preceed any demonstration with anything more predictive than "watch this"
3 REPLIES 3
Mark Grant
Honored Contributor

Re: Storing hashes of anonymous hashes

I perhaps should have mentioned that this is, of course, in perl :)
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Storing hashes of anonymous hashes

1. Do not use DB and dbmopen. That is highly deprecated
2. Use DB_File (or GDBM_File) and tie

HOH's (Hashes of Hashes) or any structure where either the key or the value is not a plain scalar cannot (directly) be stored in any of (DB|[OGN]DBM)_File

But .... we have Storable, and we have MLDBM
use http://search.cpan.org to find them

And depending on *what* is stored in the value, consider 'pack' Fast, Easy, and (once used to it) super-intuitive

use DB_File;
tie my %bus, "DB_File", "db";
$bus{Norwegian} = pack "llA10A10", $length, $height, $color, $size;

In your case Storable might be the best interface

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Mark Grant
Honored Contributor

Re: Storing hashes of anonymous hashes

Thanks procura.

I only started using "DB" because "DB_File" wasn't available on the box I was learning perl on :)

Looked into pack but couldn't seem to see where it would help but then forgot all about it once I had some kind of solution.

I'll look at your suggestions and be better for it!

I was just dissapointed that I couldn't find a use for "map()" in that example. Having said that, I imagine you'll turn my foreach into one.
Never preceed any demonstration with anything more predictive than "watch this"