Thursday, December 01, 2005
Perl Lists
Programming With Perl Lists
Introduction
In order to write really useful scripts, it's important to understand how
to work with lists. Perl has two types of lists, and I will show you how
to use them in this three part series. In this article, I'll discuss the
basic list and the functions designed to help you keep track of your data.
The multidimensional stuff is really the most important, because once you
master that you can add database programming to your web site. So let's
get started. We'll begin by taking a look at basic arrays and their
functions. Please be forewarned that the code examples in this feature are
not exciting examples of code, but are designed so that you can cut and
paste them into your scripts as needed.
There is no great mystery to an array. Every programming language has
them, and some of you may have encountered them before. In Perl they are
also called lists, and it may be easier for you to think of them this way.
They are simply an ordered list of elements, usually a string or a number.
They have a definite order, meaning that item number two is always item
number two until it is explicitly changed by the programmer or the program
itself. An example of a list are the days of the week. This is a case
where the elements of the list are in a fixed order. Another example is a
list of items in your refrigerator. The items are likely to change from
day to day, so you'd expect there to be frequent updates to you list. Both
types of lists are central to many programming tasks you will encounter.
Naming a list
To name a list, precede the variable name with '@', as in @MyList. This is
different than for scalar variables, which use '$'. A list without
elements is called an empty or null list, and is declared as follows:
@NullList = ();
Initializing a list
There are several ways to initialize the list with data. The first is to
provide a series of elements in quotes, separated by commas, and
surrounded by parentheses. It may sound like a lot, but here's what it
looks like.
@names=("Sandy", "Sally", "Sarah", "Sue");
You can also do the same thing by using the the 'qw' (quote word)
construct. It accomplishes the same thing as the example above, but uses a
different syntax. Instead of parentheses, you type 'qw/', then your data
separated by spaces, and then a trailing '/'. Here's an example,
@Animals = qw/zebra yak gnu/;
You can initialize your list using scalar variables.
# Initialize some scalar variables
$One = 1;
$Two = 2;
$Three = 3;
# Initialize the list using scalar variables
@Numbers = ( $One, $Two, $Three );
You can initialize list elements one by one. However, remember that lists
in Perl are zero-based, meaning they start at 0 and not 1. To access an
individual element, surround the index with '[]'. So if you want to
initialize the first element, you'd do it like this,
@MyList[0] = "First Element";
You can also initialize individual elements with scalar variables. We'll
keep adding to the @Numbers list we started above, so that you can see how
to add to a list.
# Initialize another scalar variable to use here
$Four = 4;
# Remember, the index to the fourth element is [3],
# because the list starts at [0]
@Numbers[3] = ($Four);
In Perl, you can also initialize a range of elements. We'll add two more
elements to @Numbers so you can see how it's done.
# Initialize a few more scalars
$Five = 5;
$Six = 6;
Now, let's add them to @Numbers.
@Numbers[4, 5] = ($Five, $Six);
You can also initialize a list by using a range, specified by '(x..y)'. In
the example below, the list will contain the same numbers as @Numbers, but
we'll initialize it in a different way.
@Numbers_2=(1..6);
Finally, you can initialize a list by using another list. Don't worry,
it's not that complicated. Say you have a list,
@FirstList = ('x', 'y', 'z');
Now you want to add it to a new list.
@SecondList = ('a', 'b', 'c', @FirstList);
If you were to print @SecondList, you'd get 'a b c x y z'. Well, actually
that brings me to my next point: how to print a list.