Hello raygun,
in effect the difference is subtle and can fade into equivalence, but see it from this point of view: a list is not a Perl data type. Perl has three main variable types: scalars, arrays, and hashes. (perlintro).
In Learning Perl - third edition is stated this way:
> A list is an ordered collection of scalars. An array is a variable that contains a list. In Perl, the two terms are often used as if they're interchangeable. But, to be accurate, the list is the data, and the array is the variable. You can have a list value that isn't in an array, but every array variable holds a list
Sometimes, just to make things more foggy, you can do operations on list in the same way as you do with array:
print +( qw(J A P H) )[2] prints P
but you cant shift a list and the doc goes: shift ARRAY but for example print accepts a list: print LIST
Another key concept is LIST in respect of context and here it overlaps with array: LIST or SCALAR context: in list context you can assign it to an array, but again, the array is the perl variable and the list is its value.
See also: What is the difference between a list and an array? and Scalars, Lists, and Arrays
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
| [reply] [d/l] [select] |
if split returned an array, you couldn't do something like ($word1, $word2, $word3, $word4) = split(/ /, 'This is a sentence.')
What am I missing in ...
@collect = split( / /, 'This is a sentence.' );
( $word1, $word2, $word3, $word4 ) = @collect;
print join( q[ ; ], ( $word1, $word2, $word3, $word4 );
... ?
| [reply] [d/l] [select] |
What am I missing
The $word* variables aren't getting assigned "an array", they are getting assigned the values from the array.
| @collect = split( / /, 'This is a sentence.' ); |
- The array on the LHS means that the assigment is in list context.
- Split returns a list of scalars.
- In list context, the elements of what's on the LHS get assigned to the values from the list on the RHS, so the @collect array is populated from the values returned by split
|
| ( $word1, $word2, $word3, $word4 ) = @collect; |
- The (...) on the LHS mean that the assignment is in list context
- Per List value constructors, the @collect array gets evaluated in list context, which results in the list (on the RHS) consisting of the elements from the @collect array
- The assignment then happens in list context, so the leftmost item of the list on the LHS ($word1) gets assigned the value of the leftmost item from the list on the RHS, and so on
|
| print join( q[ ; ], ( $word1, $word2, $word3, $word4 ); |
=> syntax error at parv.pl line 3, near ");"
but with a second ) on that line to allow it to compile:
- the words are given list context by the parentheses (unnecessarily, because join already gives list context)
- join concatenates the elements of that list, using space-semicolon-space as the separator, into a string
- That string is then printed.
|
(LHS = "Left-hand side", RHS = "Right-hand side": in these cases, relative to the assignment operator =)
| [reply] [d/l] [select] |