Welcome to the Monastery | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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
|
|