HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Storing hashes of anonymous hashes
Operating System - HP-UX
1825782
Members
2150
Online
109687
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 01:55 AM
02-03-2004 01:55 AM
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.
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"
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 01:57 AM
02-03-2004 01:57 AM
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 02:28 AM
02-03-2004 02:28 AM
Solution
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
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 02:34 AM
02-03-2004 02:34 AM
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.
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"
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Support
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP