in reply to Confused on handling merging values for hash of hashes

The confusion may arise from the fact that those things which you have called %HOH are not hashes of hashes but arrays of hashes (strictly arrayrefs of hashes). You should not try to create them with the % sigil since they are refs. So, if you said:

$aref = [ { key => 1, person => "Mike", possession => "wallet, keys, car, house, dog, cat, baseball bat" +, age => 25, }, { key => 3, person => "Dave", possession => "pony, house, car, keys", age => 21, }, ];

then you could print (or even just refer to) the possession of the second element like so:

print $aref->[1]->{possession};

HTH

Replies are listed 'Best First'.
Re^2: Confused on handling merging values for hash of hashes
by Eily (Monsignor) on Feb 04, 2015 at 13:56 UTC

    That's actually worse than that, %hash = [ { A => 1, B => 2 } ]; stringifies the arrayref to turn it into the key. So this is just a hash with a random string in it.

    hiyall, a hash is a structure that gives a name (a key) to everything it contains, so what is inside the { } is an hash (you have the value 1 with the name "A", and the value 2 with the name "B"). A hash is just a list of pairs (key and value), and lists are delimited by parentheses in perl: %hash = (A => 1, B => 2);. You can see the { } as putting the list into a box (a reference actually) that makes it easier to move around as a all instead of carrying around all the elements (this is an approximation of what happens).

    While a hash allows you to get a value by its name, an array puts values in a certain order, at a specific position. So you can make an array with a list like so: @array = (1, 2, 3, 4);. And you can have a reference to an array with square brackets [ ], this is a single element that "contains" the whole array.

    So when you write %hash = [ { A => 1 } ]; you actually put a single element (the [ ] "box") into something that expects names and values.

    Perl by default will allow you to do a lot of things, for him to warn you about this kind of mistakes, you should add:

    use strict; use warnings;
    at the top of your program.

    To understand better what you are using, and check that you get the same thing in a structure than you put there in the first place, you can try:

    use Data::Dumper; # at the top of your program my %hash = (Pablo => "Dog", Rex => "Cat", TheSpiritOfGodzilla => "Fish +"); print Dumper \%hash;

    You'll have to read some documentation if you want to go anywhere. You can try perlsyn and perldsc for a start.