in reply to Re^2: Hash of Hash
in thread Hash of Hash

I have to apology for making mistake on the question ... i have wrote "=>" but i was thinking about this "->".

From perldoc i see that have 2 ways of using the reference. One way is to use "@" and the second is using ->

" Use Rule 1 You can always use an array reference, in curly braces, in place of the name of an array. For example, "@{$aref}" instead of @array."

"Use Rule 2 Use Rule 1 is all you really need, because it tells you how to do absolutely everything you ever need to do with references. But the most common thing to do with an array or a hash is to extract a single element, and the Use Rule 1 notation is cumbersome. So there is an abbreviation.

"${$aref}[3]" is too hard to read, so you can write "$aref->[3]" instead.

"${$href}{red}" is too hard to read, so you can write "$href->{red}" instead.

If $aref holds a reference to an array, then "$aref->[3]" is the fourth element of the array. Don't confuse this with $aref[3], which is the fourth element of a totally different array, one deceptively named @aref. $aref and @aref are unrelated the same way that $item and @item are."

"Similarly, "$href->{'red'}" is part of the hash referred to by the scalar variable $href, perhaps even one with no name. $href{'red'} is part of the deceptively named %href hash. It's easy to forget to leave out the "->", and if you do, you'll get bizarre results when your program gets array and hash elements out of totally unexpected hashes and arrays that weren't the ones you wanted to use."

So in this case "my $commissions = $sales->{tuesday}{jim};"

Shouldn't $commissions have the value from $sales in this case 3, 8 or something. The confusing part from this came from that line :). Here $commissions have a reference or point to reference, and as all books and doc saying that using a reference or getting the value is to pointing or use @ in front etc ...

And you say: "The values in its key-value pairs also happen to be references to hashes" The part that also confuse me is this: "tuesday => { jim => [ 3, 8 ], mary => [ 5, 5 ] }," .... Here tuesday is a hash or reference (it must be reference because $sales can have reference in this case or im wrong? pointing to other reference anonymous (because its created using "{}" brace and have jim (again it should be reference pointing to an array in this case or reference again, reference but im confused with [] ).

BIG apology for this noob questions :S, but i really love this language, it helps me a lot and i like to get in deep of it. Learn everything and good :)

And of course thanks in advanced for helping me !

Replies are listed 'Best First'.
Re^2: Hash of Hash
by Athanasius (Archbishop) on Feb 19, 2014 at 04:37 UTC

    Hello IvanAK,

    Note: please put <code>...</code> tags around code fragments that contain square brackets (or use &#91; for [ and &#93; for ]). Otherwise, the PerlMonks site thinks you are creating a hyperlink. See Markup in the Monastery.

    Here is a subroutine that may be helpful:

    sub show_ref { use Data::Dump 'pp'; my $ref = shift; print "Reference to: ", ref($ref), "\n"; print "Contents: ", pp($ref), "\n"; }

    With this subroutine, you can easily see what any reference is, and what it contains. For example:

    my $commissions = $sales->{tuesday}{jim}; show_ref($commissions);

    gives:

    14:11 >perl 879_SoPW.pl Reference to: ARRAY Contents: [3, 8] 14:11 >

    which shows that $commissions is a reference to an array which contains the elements 3 and 8.

    Here tuesday is a hash or reference

    No, “tuesday” is a hash key, and therefore it is a string (because in Perl, hash keys can only be strings). A hash contains key-value pairs, and in this case the value paired with the key “tuesday” is a reference to a hash. So show_ref($sales->{tuesday}); gives:

    14:11 >perl 879_SoPW.pl Reference to: HASH Contents: { jim => [3, 8], mary => [5, 5] } 14:25 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,