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.
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?my @array = (1,2,3,4,5);
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:
but that, too, isn't definitive, because $_ *= 2 for $x, $y, $z works just fine.$_ *= 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
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 |