$hash and %hash are 2 completely different variables. You can have a @hash that is an array if you really wanted to go nuts :-) ...

Look at the difference that dereferencing with the first -> makes:

use strict; use warnings; use feature 'say'; my $hash_ref = {}; # SCALAR pointing to an empty HASH reference my %hash = (); # actual HASH $hash{key1}{key2} = 'val1'; $hash_ref->{key1}->{key2} = 'val2'; $hash_ref = { # HASH reference, note: curly brace *** key1 => { # key1 is a nested HASH reference key2 => 'val2', # key2 is pointing to the value "val2" }, }; %hash = ( # actual HASH, note: parens *** key1 => { # key1 is a nested HASH reference key2 => 'val1', # key2 is pointing to the value "val2" }, ); # accessing "key2" in $hash_ref: say $hash_ref->{key1}->{key2}; # the first "->" is important say $hash_ref->{key1}{key2}; # accessing "key2" in %hash: say $hash{key1}{key2}; # lack of the first "->" is important say $hash{key1}->{key2}; __END__
Output:
val2 val2 val1 val1
To underscore the difference also, you can look at how an a HASH can be assigned with an even sized list,
my @hash = ("key3", "val3", "key4", "val4"); # actual ARRAY my %hash3 = @hash; # even sized (2, 4, etc) list assignment works say $hash3{key3}; say $hash3{key4};
Output:
val3 val4

In reply to Re: Hash syntax by perlfan
in thread Hash syntax by mvanle

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.