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.


In reply to Re^2: Confused on handling merging values for hash of hashes by Eily
in thread Confused on handling merging values for hash of hashes by hiyall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.