Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Confused on handling merging values for hash of hashes

by Eily (Monsignor)
on Feb 04, 2015 at 13:56 UTC ( [id://1115520]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found