Looking at the book, I see that chapter 6 is on references. This is not the simplest area of Perl, but it's worth understanding because of its power. The title of your node makes me think that you may be slightly confused. In your first code snippet, $sales is a reference to an anonymous hash of hashes of arrays. It's not a simple hash of hashes (or HoH). One of the monastery's most highly rated tutorials is References quick reference, which may help you.

To try to explain the second snippet in more detail, let's start with the first line: my $commissions = $sales->{tuesday}{jim}. This creates a reference to an array, the array being the element of the anonymous HoH containing Jim's commissions on Tuesday.

The next line gives the number of elements in the array referenced. If you understand that $commissions is an array reference, References quick reference will explain that using the combined @$ sigils will derefer the reference, so that you are returning the array itself. However, you are returning it in "scalar context". Context is something else that often confuses people, me included (just look through my writeups). In scalar context, this returns the number of elements in an array. The latest versions of Perl have a function that makes this explicit, so you might say my $elements = scalar(@array);.

Compare this to the use of @$commissions in the fourth line of your second snippet. Here, the array is being used in list context, meaning that each element of the array will, in turn, be put into the default variable $_. Line 5 will add them to $total, which was initialised to 0 in line 3.

Hope this helps. I also hope that my understanding is correct. If anyone says I've got it wrong, believe them.

Regards,

John Davies


In reply to Re: Hash of Hash by davies
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.