Re: difference between array and list in perl
by toolic (Bishop) on Jan 16, 2010 at 18:13 UTC
|
| [reply] |
Re: difference between array and list in perl
by LanX (Saint) on Jan 16, 2010 at 18:49 UTC
|
A lot of theories are plain wrong, even the docs are not consistent...(e.g. naming "wantarray" instead of "wantlist")
An "Array" is a variable with allocated space in memory (noted/accessed with @x or $x[...] or [a,b,c] resp.) a "List" is either only syntax in the source, i.e. comma seperated expressions² or the mechanism for passing multiple values (e.g. when calling and returning from a sub¹) !
So your example
@list2 = (list1, list2, list3, list4); #example of list (WRONG!)
Shows a list at RHS whose values are assigned to an array-variable LHS.
Unfortunately in perl5 it's not simply possible to identify a list as a "literal array", because there are plenty of subtle differences, eg with @A=(a..c) these snippets $a=@A and $a=(a,b,c) produce totally different results.
UPDATE: Technically there is nothing like a "literal array", even if you construct an anonymous array reference [a,b,c] you have a literal list a,b,c used to initialize an array!
You may want to search in the archives of the monastery ... it's really a f.a.q. ...
1) The values are passed on a internal stack which exists only temporarily.
2) The parens are only for grouping.
UPDATES: different clarifications... | [reply] [d/l] [select] |
|
|
"wantarray" is a misnamed feature, but it is documented accurately ("... looking for a list value").
| [reply] |
|
|
as I already said ...the docs are not consistent...(e.g. naming "wantarray"...
| [reply] |
Re: difference between array and list in perl
by cdarke (Prior) on Jan 16, 2010 at 21:35 UTC
|
Calling a list a speial type of array is a little misleading. An array is a variable, a list is not. In your example: @list1 = (1,2,3,4);
@list2 = (list1, list2, list3, list4);
@arr1 = ("1" , "2", "3", "4");
@arr2 = ("arr1", "arr2", "arr3", "arr4") ;
@list1, @list2, @arr1, and @arr2, are all arrays. In all your examples the right hand side of the assignments are lists, although the list (list1, list2, list3, list4) is questionable, since it contains bare words which might be interpreteted as subroutine calls. To ensure text you can autoquote each word using qw, for example:my @arr3 = qw(value1 value2 value3 value4);
qw returns a list (in an old release it used to return an array, and that was considered to be a bug).
Fortunately you can assign the items in a list to an array, as you have. When we do this kind of assignment it is essentially a copy of the items on the right hand side into elements in the array.
You can assign to a list on the left side, provided the list only consists of variables (although undef is a special case). For example:my @arr5 = qw (quick brown fox);
my ($speed, $colour, $animal) = @arr5;
will assign the three values to the array, then assign from the array into the three variables $speed, $colour, $animal arranged in a list. | [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
>perl -MO=Concise -e"$x = (4,5,6)" 2>&1 | find "list"
5 <@> list sKP ->6
^ ^
| |
list scalar context
That's why it's clearer to say "an expression cannot return a list in scalar context". In the above example, the list evaluates to the scalar 6.
Since you know better, why do you use such a needlessly confusing sentence?
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
Re: difference between array and list in perl
by matze77 (Friar) on Jan 16, 2010 at 18:10 UTC
|
I think it is also important to remember when "list context" and "scalar context" is "used" This holds surprises for me every time i do it wrong again ;-)...
http://perl.plover.com/context.html
| [reply] |
Re: difference between array and list in perl
by ikegami (Patriarch) on Jan 17, 2010 at 07:36 UTC
|
Lists are really a special type of array - .essentially, , a list is a temporary construct that holds a series of values. The list can be "hand" generated using parentheses and the comma operator,
There are many definitions of list, so it's hard to describe what it is. Among other things, it can refer comma separated values in the source (a list literal), the operator produced by that code (a list operator) or the values the operator places on the stack (a list value).
On the other hand, the definition of array is quite clear. First and foremost, it's a type of variable. Very few if any properties are shared between arrays and lists (whichever definition you use) since lists are never variables in Perl.
Is it like when an array is created without any double quotes or single quotes, its called list ?
An array is never called a list. An array is never a list. It may return one. A list may be assigned to one.
In all of your examples, you are assigning a list to an array. You could also say you are initialising an array.
arrays lists
vvvvvv vvvvvvvvvv
@list1 = (1,2,3,4);
@list2 = (list1, list2, list3, list4);
@arr1 = ("1" , "2", "3", "4");
@arr2 = ("arr1", "arr2", "arr3", "arr4");
^
assignments
| [reply] [d/l] |
Re: difference between array and list in perl
by WizardOfUz (Friar) on Jan 16, 2010 at 18:00 UTC
|
| [reply] |
Re: difference between array and list in perl
by ivo_ac (Acolyte) on Jan 17, 2010 at 14:56 UTC
|
In my understanding (and as explained by "Learning perl"), an array is a variable containing the list.
So the list is the data itself, the array is the variable in which it is stored.
| [reply] |
|
|
Yes, you can say an array contains a list (of elements). I just wanted to warn against making the incorrect assumption that lists are only found in arrays.
| [reply] |