note
dmmiller2k
<P>Just to add my voice to this cacophany ...</P>
<P>
If I recall my own confusion on this point correctly, and the explanation that helped me finally turn the corner (toward understanding), a "list" may be described as a <I>context</I>, or a way of interpreting "thingys" (or variables), rather than as a thingy in its own right, whereas an array is one of three types of containers (thingys) your program may use to hold other thingys (information).
</P>
<P>
For example, an array, <CODE<>@a</CODE>, containing the three values, 'a', 'b' and 'c' (or <CODE>@a = ('a', 'b', 'c')</CODE>); in <I>scalar</I> context (in other words when the context <U>requires</U> a single value), it will evaluate as 3 (its number of elements). An example of this is a simple assignment to a scalar:
</P>
<CODE>$x = @a; # will contain 3</CODE>
<P>
On the other hand, in <I>list</I> context (or, when the context <U>permits</U> multiple scalar values), the array will be <I>flattened</I> into a series of values of its elements, as in the classic case, when it is passed as a parameter to a (non-prototyped<SUP>1</SUP>) sub:
</P>
<CODE>
my_sub( @a );
# ...
sub my_sub {
# here the localized @_ variable contains references to
# the list of values passed as paramters,
for my $i (0 .. $#_) {
print "param $i = $_[$i]\n";
}
}
# OUTPUT
# param 0 = a
# param 1 = b
# param 2 = c
</CODE>
<P>
(Hashes may also be similarly flattened into lists, where the key/value pairs are guaranteed to remain adjacent and in 'key', 'value' order, but the ordering of the pairs is not necessarily deterministic.)
</P>
<P>If array thingys and hash thingys and scalars are all intermingled in a given list context, they are all flattened into a single list of all the individual elements:
</P>
<CODE>
@a = ( 'I', 'am' );
%h = ( 'perl' => 'hacker' );
$x = 'just';
my_sub( @a, $x, 'another', %h );
# OUTPUT:
# param 0 = I
# param 1 = am
# param 2 = just
# param 3 = another
# param 4 = perl
# param 5 = hacker
</CODE>
<P><SUP>1</SUP> Prototypes can alter this behavior, but that's a whole 'nother conversation.</P>
<P>dmm</P>
<FONT FACE="Arial,Helvetica,sans-serif" SIZE="-1">
<I>You can </I>give<I> a man a fish and feed him for a day ...<BR>
Or, you can </I>teach<I> him to fish and feed him for a lifetime</I></FONT>
130861
130864