Hello morgon,

I would have expected $y also to be 2, but instead the last element of the list is used instead...

But there is no list. I think what’s confusing here is that parentheses are used in Perl to make lists. But that only works in list context. In scalar context, as here, parentheses are only for grouping,1 which in this case makes no difference:

13:20 >perl -wE "my $y = ('a', 'b'); say $y;" Useless use of a constant ("a") in void context at -e line 1. b 13:20 >perl -wE "my $y = 'a', 'b'; say $y;" Useless use of a constant ("b") in void context at -e line 1. a 13:20 >

From perldata#List-value-constructors:

List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it):
        (LIST)
In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element, as with the C comma operator.

Update: 1Actually, parentheses are only used for grouping (precedence), regardless of context, as indicated in the perldata quote. My statement that parentheses are used to make lists is a little misleading: they are generally needed, but only because the assignment operator has higher precedence than the comma operator:

14:05 >perl -MData::Dump -wE "my @a = 't'; dd \@a;" ["t"] 14:13 >perl -MData::Dump -wE "my @a = 't', 'u'; dd \@a;" Useless use of a constant ("u") in void context at -e line 1. ["t"] 14:13 >perl -MData::Dump -wE "my @a = ('t', 'u'); dd \@a;" ["t", "u"] 14:13 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: arrays and lists, hmmm by Athanasius
in thread arrays and lists, hmmm by morgon

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.