Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with hash/database

Status
Not open for further replies.

hd65

Technical User
Aug 17, 2000
27
US
HI,
I can’t figure out how to do this:
I need to build a database like this,
key = colors=>
color1 => red
color2 =>blue
color3 => black
ect…
key = shapes=>
shape1 => oval
shape2 => square
shape3 => circle
ect…

ect...
I need to search on key and return each item separately when called.
Would this be a multi dimensional hash? If so then would I have to use MLDBM ?
I’ve tried several different approaches with no luck
if this is something simple I apologize,
but I just can’t get it.
thanks
paul
 
I think you just want 2 hashes; one for colors and one for shapes. The following will create a hash.
Code:
%colors = {
            color1 => 'red',
            color2 => 'blue',
            color3 => 'black',
};
Then individual colors can be accessed like this:
print "$colors{color2}"; # prints blue

FYI, a multi-dimensional hash (or a hash of hashes) is a record that contains other records. Here's an example:'
Code:
%student = {
        Robert => {
            lastname => "Anderson",
            birthday => "12/12/84",
        },
        Karen => {
            lastname => "Smith",
            birthday => "1/1/85",
        },
};

To print Karen's birthday: 
print "$student{Karen}{birthday}";

 
It does look like you need a "hash of hashes" approach. Raider gave you the basic syntax for that. You can extend that to as many levels as you require. From the looks of things, you need a top-level hash to store the "key" values in too. If this isn't going to be too large, and if you aren't going to be changing it a lot, instead of using a "database" or MLDBM, just create a .pl file containing the definition and "require" it into your program when you need it. If you do this, don't forget to end the file with a line containing just
Code:
1;
to make sure that the require returns "true".
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon,
First, thanks for the ideas.
Second, "not too large"??? it may be rather large
but it may not be changed very often.

All the values will be user inputs, basicly the database will be built by the user.Can that be done with the *.pl file method?

I have other items to work on for this project so I thought I would just build a few hashes to hold the data(until the program is exited) and not put too much effort in the database as the problems were frustrating me.
but if you have time could you just give me a simple example of using hash of hashes and a database? I tried to go through the MLDBM doc. but just could not get it. I think if I could get a working example it would help me with understanding "how and why" much better.

thanks
paul
 
Here is an example of playing with a hash of hashes.
Code:
%families = (
    smith => { 
        husband => 'bill',
        wife    => 'sally',
        kid1    => 'bill, jr',
        kid2    => 'missy'
        },
    cleaver => {
        husband => 'ward',
        wife    => 'june',
        kid1    => 'wally',
        kid2    => 'beaver'
        },
    haskel => {
        kid1    => 'eddie',
        }
    );

# add a member to the smith family
$families{'smith'}{'kid3'} = 'bill, III';

# knock off beaver
delete $families{'cleaver'}{'kid2'};

foreach $k (keys (%families))
    {
    print "family: $k \n";
    foreach $k2 (keys %{$families{$k}})
        {
        print "\t$k2 - $families{$k}{$k2}\n";
        }
    }

# put beaver back and print the cleavers
print "Cleavers, again:\n";
$families{cleaver}{kid2} = 'beaver';
foreach $k (keys %{ $families{'cleaver'}})
    {
    print "\t$k $families{'cleaver'}{$k}\n";
    }

Also, in 'Programming Perl', 2nd Edition, by Wall, Christiansen,
& Schwartz, published by O'Reilly, on page 270 and for several
pages thereafter, there are several clear examples of creating
multi-dimensional hashes. If you don't have that book or a later
edition, you might want to get it.

HTH If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Hi,
I don't have any problems using hashes...
the problems start when I try to tie the hash to a dbm file
and add, edit and read from the file using MLDBM.
I've read the MLDBM doc over and over yet still can't get it.
thanks
paul
 
I can't help much with MLDBM. I use MySQL for most of my database needs. You can use the .pl method when the user creates the file, just write out the file like you would any other text file, being very careful about escaping special characters. The generic quoting commands are great for this (i.e. print qq[This can contain "unescaped" quotes];) It's even pretty easy to update. You require your hash of hashes, update it "in-memory" by simply applying the changes to the hash(es), and then iterate thru it and rewrite it to the .pl file. Even if it's pretty large this can be rather effective. However, if it gets too complex I'd look into using a REAL database like MySQL. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon,
thanks I'll have to give it try.
paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top