in reply to Re: reference array and pass ref to sub
in thread reference array and pass ref to sub

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".

Replies are listed 'Best First'.
Re^3: reference array and pass ref to sub
by Marshall (Canon) on Mar 07, 2009 at 01:57 UTC
    I think the distinction between a "C" 1D array and a Perl list is getting missed here. In "C" a 1D array is of fixed size. In "C" if we want to take say the 3rd item out of an array, we have to do something about that "missing 3rd place". If this this was 6 elements before, then we have to "shrink it" and that process is expensive. In Perl, we can just take the 3rd thing out and that's it!

    The most fundamental dynamic data structure in C, the linked list is not necessary in Perl! Its built into the language!

    The Perl terminology is very clear when it comes to multi-dimensional structures. In "C" if I asked for a picture of a 2-D array, you couldn't do it! Because you'd have to ask me more questions, like do you want a traditional 2D array or a more practical 2D matrix (which would be an array of pointer to array).

    Basically Perl removes one level of complication from complex structures. Since doubly linked "C" lists and hash table are built into the language, amazing things can happen!