Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Hash of Hash

by Athanasius (Archbishop)
on Feb 18, 2014 at 15:19 UTC ( [id://1075350]=note: print w/replies, xml ) Need Help??


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

Hello IvanAK,

And also we have difference between reference and anonymous reference (or not ? but i see a little difference ).

Not really. You will find the built-in function ref useful here. Consider:

#! perl use strict; use warnings; my %hash = ( Fred => 'Wilma', Barney => 'Betty' ); my $ref1 = \%hash; my $ref2 = { Howard => 'Marion', Richie => 'Lori Beth', Chachi => 'Joa +nie' }; printf "\$ref1 is a %s\n", ref $ref1; printf "\$ref2 is a %s\n", ref $ref2;

Output:

0:52 >perl 879_SoPW.pl $ref1 is a HASH $ref2 is a HASH 0:52 >

As you can see, there is no intrinsic difference between the first reference, which is to a named hash, and the second, which is to an anonymous hash. The difference is in the way the hashes are created, not in the references to them.1

My enigma here is what kind of reference or anonymous thing is $sales.

$sales (a scalar variable) contains a reference to a hash. That hash happens to be an anonymous hash, because it was created that way. The values in its key-value pairs also happen to be references to hashes (and, again, those hashes were created anonymously). But that has nothing to do with what $sales is — simply, a reference to a hash.

The “fat comma” operator => has nothing to do with creating references, it is just a convenient replacement for the comma which (1) stringifies the left-hand-side (under certain conditions) and (2) “is helpful in documenting the correspondence between keys and values in hashes, and other paired elements in lists.” — see Comma Operator.

Hope that helps,

1Well, OK, there is an important difference in the reference counts of the hashes: for %hash the reference count is 2, for the anonymous hash referenced by $ref2 it is 1. But that applies to the underlying data structures, not to the reference values held by $ref1 and $ref2.

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

Replies are listed 'Best First'.
Re: Hash of Hash
by IvanAK (Acolyte) on Feb 19, 2014 at 00:13 UTC

    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 !

      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,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found