As i learn more about this reference thing here is what iv got. And this is all from perldoc and the pages you post here davies
A reference is a scalar value that refers to. We all know that. The part that clear a 10% :) is: "You can't have a hash whose values are arrays; hash values can only be scalars. We're stuck with that. But a single reference can refer to an entire array, and references are scalars, so you can have a hash of references to arrays, and it'll act a lot like a hash of arrays, and it'll be just as useful as a hash of arrays."
And also we have difference between reference and anonymous reference (or not ? but i see a little difference ). As perldoc say with this $aref=\@array; # $aref now holds a reference to @array $href = \%hash; # $href now holds a reference to %hash we create a reference on a array and hash. $aref and $href point to them and as they are "references are scalars".
So anonymous array or hash is (as i understand) a reference that have many reference, and those many reference can be array hash or what ever we want or what ever we point. ( write ? ) :)
If we have simple array and we want to make reference and use it we make something like this...
my @arr = (1,2,3,4); print "This is array: @niz\n"; my $ref = \@niz; print "This is the reference: $ref\n"; print "And this should be dereferencing or getting the value of the ar +ray through the reference : @{$ref}\n"; # This is same as (print "Thi +s is array: @niz\n";)
or for hash
my %h = ('Ivan'=>7, 'AK'=>8,); print "$h{'Ivan'}\n"; my $refh = \%h; print "$refh\n"; print "${$refh}{'Ivan'}\n"; print "$refh->{'Pero'}\n";
A simple experiment :D
So lets go back to the task from the book and the solution We have this code:
As we can see we have some anonymous hash reference named $sales. And with this first line of code my $commissions = $sales->{tuesday}{jim}; It is a reference to the hash reference of Thursday that have hash of jim ?!? ( or something ?As you say "This creates a reference to an array, the array being the element of the anonymous HoH containing Jim's commissions on Tuesday." im confusing with "=>" this ... is it a hash or array or hash that has array reference or wtf :D) inception :D :D And about $num_sales i think i got it ... if we put @ in front of reference we get the values from it and because we get the value but this value is array and we are putting it in scalar that gave us the number in array, the number of elements that the array have (something like that in my mind :D ) the other think is typical counting the element for loops etc ... that i know from other languages :D ...my $sales = { monday => { jim => [ 2 ], mary => [ 1, 3, 7 ] }, tuesday => { jim => [ 3, 8 ], mary => [ 5, 5 ] }, wednesday => { jim => [ 7, 0 ], mary => [ 3 ] }, thursday => { jim => [ 4 ], mary => [ 5, 7, 2, 5, 2 ] }, friday => { jim => [ 1, 1, 5 ], mary => [ 2 ] }, }; my $commissions = $sales->{tuesday}{jim}; my $num_sales = @$commissions; my $total = 0; foreach (@$commissions) { $total += $_; } print "Jim made $num_sales sales on Tuesday and earned \$$total commis +sion\n";
My enigma here is what kind of reference or anonymous thing is $sales.
In reply to Re: Hash of Hash
by IvanAK
in thread Hash of Hash
by IvanAK
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |