As long as we're trying to get the terminology right, Perl has both lists and arrays. And they're different. And it's kind of annoying since their differences are fairly minor. I don't know of any other language that makes this distinction - that's not to say they don't, but I just don't remember one doing so.

my @array = (1,2,3,4,5);
Here, @array is an array, and (1,2,3,4,5) is a list (of five elements: the numbers 1 through 5). They are different. For example, it wouldn't really be a list without the parenthesis, it'd be five numbers with the comma operator in between, which would then evaluate to just the first one (due to the precedence of the comma operator). Of course, if it were "1..5" instead, then the parenthesis isn't needed. Or if it were in some other list context, such as print for 1,2,3,4,5, then the parenthesis still isn't required. Got that straight yet?

Ok, another difference is in the way you can take a reference: if you try \1,2,3,4,5, that's just not going to do anything sensical. How about \(1,2,3,4,5)? Well, that does something really groovy, but it's not the same as [1,2,3,4,5] (which is what it'd look like if you did \@array when @array contains 1..5).

Finally, a bigger difference is in what happens when you try to modify it:

$_ *= 2 for 1,2,3,4,5; # doesn't work because you're modifying constan +ts. @a = (1,2,3,4,5); $_ *= 2 for @a; # modifies @a because @a has copies of the constants i +nstead of the constants themselves
but that, too, isn't definitive, because $_ *= 2 for $x, $y, $z works just fine.

Personally, I don't see the big deal. There are already plenty of rules here, and delineating between lists and arrays isn't really required for understanding the language (I was writing perl for a few years before joining this site and finding out here). Mostly, we care about lvalues and rvalues. Maybe it's my C/C++ background showing, but I think that's where we should be concentrating on the differences more than "array" vs "list".


In reply to Re^2: reference array and pass ref to sub by Tanktalus
in thread reference array and pass ref to sub by csarid

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.