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 | |
by Athanasius (Archbishop) on Feb 19, 2014 at 04:37 UTC |