| Previous | Table of Contents | Next |
Scalars contain just one value. Arrays (also called lists) contain multiple scalars glued togetherthey can hold many values or none at all (Figure 1-7). Array variables begin with an at sign (e.g., @movies or @numbers), and their values are represented as a comma-separated list of scalars enclosed in parentheses.
Figure 1-7 An array
@movies = (Casablanca, Star Wars, E.T.);
is an array of three strings.
@numbers = (4, 6, 6, 9);
is an array of four numbers.
$var = 7; @values = ($var, $var, 2, $var + 1)
sets @values to (7, 7, 2, 8).
Functions such as print() actually expect an array of arguments. Thats why you separate them with commas, as in
print $greeting, world!, \n;
or
print(5, 6, 7);
or even
@array = (5, 6, 7); print @array, \n;
You can mix data types in an array
@numbers = (6, 7, nine); @years = (1996.25, two thousand, 1066);
and combine multiple arrays into one.
@things = (@numbers, hello, @years); # @things now has seven
elements.
Heres an array with no elements at all:
@empty_array = ();
Lets switch gears a little bit and explore another loop construct: foreach. The marquee program, shown in Listing 1-13, uses an array to store a list of movies and a foreach loop to print all of them.
Listing 1-13 marquee: Initializing and looping through an array
#!/usr/bin/perl -w
@movies = (Casablanca, Stripes, Gremlins, Sleeper);
print Now showing at the Perlplex:\n;
foreach $movie (@movies) {
print $movie, \n;
}
The first line of marquee initializes the array @movies so that it contains four strings. Then the foreach statement steps through @movies, changing $movie to the current array element and printing it. The result:
% marquee
RESULT: Now showing at the Perlplex:
Casablanca
Stripes
Gremlins
Sleeper
The foreach Loop
foreach SCALAR ( ARRAY ) { BODY } executes the statements in BODY once for every ARRAY element. The current array element is placed in SCALAR.
When the foreach loop begins, $movie becomes the first array element: Casablanca. The BODY prints it, and when the loop begins again, $movie is set to the next array element: Stripes. This continues until @movies runs out of elements.
If @movies had been undefined or had been set to an empty array ( ), the BODY would have been skipped over entirely.
All arrays are ordered: You can speak of the first element, or the third, or the last. Like C, Perl uses zero-based indexing, which means that the elements are numbered beginning with zero. So if the array @movies has four elements, the first element is number 0, and the last element is number 3. Its important to remember this when accessing an arrays elements.
There are a few different ways to access the elements of an array. This can get a little confusing; the important thing to remember is that you can always tell what something is by its identifier: Anything beginning with a $ is a scalar and anything beginning with a @ is an array.
@movies = (Casablanca, Star Wars, E.T., Home Alone);
@movies2 = @movies;
$movies[1]
$movies[3] = Home Alone 2;
@movies[1..3]
@movies[0..2] = (Big, Jaws, Batman);
(Big, Jaws, Batman, Home Alone 2)
The .. operator creates an implicit array: 1..4 is (in most ways) equivalent to (1, 2, 3, 4). You can use this operator to loop through a range of numbers.
% perl -e foreach $num (2..9) { print $num; }
RESULT: 23456789
The syntax for accessing scalars and arrays isnt arbitrary. Remember the cardinal rule: You can identify whats being accessed by the initial character. Single pieces of data (scalars) always begin with a $ (such as $var, $array[2]), and multiple values (arrays) always begin with an @, such as @array, or @array[0..2], or even @array[7], which is an array that happens to contain just one element. Dont be misled by the single element inside the brackets: @array[7] is NOT a scalar!
Other data types, introduced in the next four chapters, use different symbols: & (subroutines), % (hashes), and * (symbol table entries).
What happens if you take the four-element @movies and assign elements beyond the end of the array? You know that the last element of @movies is $movies[3]. If you say
@movies[3..5] = (Doctor Detroit, Grease, Saturday Night Fever);
then Perl automatically extends @movies for you.
| Previous | Table of Contents | Next |