in reply to Using multi-level hashes

If you really need to type things in manually, try this:
my %hash = ( "Abe" => { "lastname" => "Lincoln", "friends" => { "George" => { "lastname" => "Washington", "wife" => "Martha" } "James" => { "lastname" => "Madison", "wife" => "Dolly" } }, }, "Franklin" => { "lastname" => "Roosevelt", "wife" => "Eleanor" } )
However, if you have some structure, you'll want to stuff these programmatically, instead of retyping all of the keys.

There are several ways to do this. The most direct is to create a sub to navigate the hash for you. The sub will take a hash ref, a value, and a list of keys. The value may itself be a hash(ref), so you can nest these and use loops and so forth.

For example:

#!/your/perl/here use strict; use warnings; use Data::Dumper; sub update_hash { my ($h, $value, @keys) = @_; for my $key (@keys[0..$#keys-1]) { $h = $h->{$key}; } $h->{$keys[-1]} = $value; } # build up a hash from scratch my %hash; update_hash(\%hash, {}, 'one'); update_hash(\%hash, {}, 'one','two'); update_hash(\%hash, 123, 'one','two','three'); # don't have to start at the top update_hash($hash{one}{two},129,'nine'); update_hash($hash{one}{two},{},'zero'); update_hash($hash{one}{two},1208,'zero','eight'); print Dumper(\%hash); __OUTPUT__ $VAR1 = { 'one' => { 'two' => { 'three' => 123, 'zero' => { 'eight' => 1208 }, 'nine' => 129 } } };
The OO approach is really just a group of setters/getters that grok your data structure. If you have a complicated and deeply-nested structure, you might want to do that just for your sanity -- otherwise, it's probably better just to write subs as needed.

-QM
--
Quantum Mechanics: The dreams stuff is made of