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:

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";
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 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

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.