Hi all, Im learning perl and i start reading the book "beginning perl curtis ovid poe" and i stop at the chapter 6 EXERCISES on second task. The task is: "Write the code to find the individual number of sales Jim made on Friday and the total number of the sales he made on Friday. Assume each number is the total for an individual sale."

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 ] }, };

What i found as solution is this

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

the thing that is confusing me is this part ...  my $commissions = $sales->{tuesday}{jim} and $num_sales = @$commissions From the book, to access the element of HoH you using something like my $num_sales = $sales->{friday}{mary}; but in this example, on the first line my $commissions = $sales->{tuesday}{jim}; the commissions have a reference value not the value itself. (when you try to print gave me something like ARRAY(0x94f2c3c). But if i print with @ in front like this: print "@$commissions"; it gave the value that jim have on Tuesday. From the book we have this:

my @fools = qw(jester clown motley); my $fools = \@fools; The $fools variable now contains a reference to the @fools array. You +can copy the values to another array by prepending it with the @ sign (the array sigil). my @copy_of_fools = @$fools; To access individual elements of the $fools array reference, you use t +he same syntax as you would to access the original array, but you use the dereferencing operator, +->, between the array name and the square brackets.

And this is for array. And this is one of the problem. Should $commissions have to have the value of jim already ? And how in the name of PERL :) $num_sales = @$commissions get the number of elements in the array or hash or i thing here i have another problem of dont know what this $sales is (scalar yes but what have inside :) Hashes or arrays by telling of using [] ? Sorry for the english :) !


In reply to 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.