Beefy Boxes and Bandwidth Generously Provided by pair Networks
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
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found