$_ iterates through that list (but why wasn't my original say for iterating through the list?)

@ dereferences the array (again, why didn't my original @{...} dereference it?)

It was iterating through the list and it did dereference , but the number 2 is not an array reference, so the list was empty :) strict noticed this problem, so it ended your program . This is why we love strict.

Whatever text you're learning from, you need to go back and do the exercises you skipped :) the only way to absorb and remember is by typing it yourself :)

$ perl -E " say for [1..4],[5..8] " ARRAY(0x3f8adc) ARRAY(0x9b9eac)
aww, array references stringify like that, to get at the values, we need to dereference

Trying it your way

$ perl -E " say for @{ [1..4],[5..8] }" 5 6 7 8
Aww, it only printed the contents of the last reference. To print both, we need to iterate over a list of reference, not a list of values
$ perl -E " say @$_ for [1..4],[5..8] " 1234 5678
Oh look, the numbers are all joined together, thats how say works
$ perl -E " say 1,2,3,4; say 5..8; " 1234 5678
I know, we'll put it in double quotes string, that will improve it
$ perl -E " say qq{@$_} for [1..4],[5..8] " 1 2 3 4 5 6 7 8
Now for the brackets
$ perl -E " say qq{[@$_]} for [1..4],[5..8] " [1 2 3 4] [5 6 7 8]
Now for the comma by changing the $", the LIST_SEPARATOR (the \" is for my shell, cmd.exe )
$ perl -E " local $\"=q{,}; say qq{[@$_]} for [1..4],[5..8] " [1,2,3,4] [5,6,7,8]
Even if we employ $" properly by limiting the scope of the changes with local, its better to use join function
$ perl -E " say q{[}, join( q{,}, @$_ ), q{]} for [1..4],[5..8] " [1,2,3,4] [5,6,7,8]
The values function is context sensitive
$ perl -E " %f = 6..9; say for values %f; " 9 7 $ perl -E " %f = 6..9; say for scalar values %f; " 2
Using @{} puts values %f in scalar context, just like the scalar function. This makes the values function return the number of values in %f; The number of values is not an array reference.

To learn more about context see Tutorials: Context in Perl: Context tutorial, or see the free book Modern Perl, a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.


In reply to Re^7: Dereferencing a Hash of Arrays by Anonymous Monk
in thread Dereferencing a Hash of Arrays by toro

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.