more useful options | |
PerlMonks |
Re^2: Confused on handling merging values for hash of hashesby Eily (Monsignor) |
on Feb 04, 2015 at 13:56 UTC ( [id://1115520]=note: print w/replies, xml ) | Need Help?? |
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: 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:
You'll have to read some documentation if you want to go anywhere. You can try perlsyn and perldsc for a start.
In Section
Seekers of Perl Wisdom
|
|