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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |