Perl Variables: Lists (aka Arrays)
April 26, 1999
So far we've been deluged with information on scalar
variables, certainly the most commonly used type of variable
in Perl (or any other programming language for that matter).
Runner-up in this popularity contest, though, are lists, also
commonly known as arrays in other languages. Lists, in brief,
allow one variable label to contain a bunch of values, rather
than merely one.
We use list variables frequently in real life. A shopping list
contains a bunch of values -- names of items. A budget is a
list of arithmetic values -- expenditures.
List variables are quite different from scalar variables because
they do not share the same types of operations. After all, it
doesn't make much sense to compare whether a list of colors is
"greater" than a list of pet names (it does
make sense to compare whether one list is longer than
another, but then the length of a list is a scalar variable).
To prevent variable name confusion, lists in Perl are prefixed
with the at-sign character (@). The values contained in a list
are called items, and assigning a list of items to a list is
quite simple:
@shopping=("milk","eggs","bread","cheetos");
Now then, the list @shopping contains four items, indexed in
the order they were created. The first item in the list is
"milk", the second is "eggs", and so on.
Important: the first item in a list is counted as
index number zero. Restated, item 0 in this list
is "milk", item 1 is "eggs", and so on.
Here's the confusing bit: when working with lists, we sometimes
work with the list as a whole and other times work with
particular items from the list. Working with a single item
from a list is a scalar activity, as we saw in the earlier
sections. You can reference a single item from a list using
its index:
$shopping[0]="skim milk";
The above variable refers to the first item in the @shopping
list, which has been assigned a new value, "skim milk".
Notice, though, that this reference used a $ rather than
an @ -- because this one item in the list is scalar. What
if you wanted to work with more than one item from the list,
but not the whole list? That, then, would constitute a
slice, because it is still a list (a list of a list!):
@shopping[0,3]=("skim milk","diet cheetos");
In this example we've assigned new values to two of the items
in the list, the first and last items. This time we stuck
with the @ prefix, because we have used a slice of the
list.
It is quite important to use the correct notation, scalar or
list, when constructing these references. Sometimes, using the
wrong notion, such as a $ where an @ should be, won't result in
an error, per se, but in incorrect results within the program.
This is because Perl will interpret the expression one way or
another depending on whether you intended to "see"
the variable as a list or a scalar at a given moment.
You can determine the length of a list -- that is, how many
items it has, using the scalar reference:
$#shopping
The above returns the number of the highest index in the
list; in this example, that would be 3. Remember that the
list begins at index 0, so a high index of 3 means that this
list contains 4 items. If you've ever wondered why programmers
are, well, the way they are ... just remember that they have
to deal with this kind of thing day in and day out!
Lists provide a convenient means for many types of data sorting,
but, again, this tutorial is very much a starter course. Once
familiar with the basics of these Perl fundamentals, the twists
and turns that come down the road will be that much less dizzying.
Table 2. Perl Comparison Operators
The Perl You Need to Know
Perl Variables: Hashes
|