Previous Table of Contents Next


The Last Index of an Array

The scalar $#array contains the last index of @array. If @array has four elements, then $#array is 3. You can always access (or modify) the last element of an array with this notation:

$movies[$#movies] = ‘The Last Seduction’

sets the last element of @movies to ‘The Last Seduction’, regardless of how many elements @movies contains.

And you can extend an array by setting this value. After saying

$#movies = 10

@movies will have ten elements, no matter how many it had before. Any new elements created in this way will be undefined but will exist anyway. (There’s a subtle difference between existing and being defined, explained further in Chapter 5, Session 5.)

The $[ Special Variable

$[ is a special variable. You’ll see a slew of special variables throughout the book. This particular variable is a scalar (but of course, you knew that from the $) containing the first index of all arrays. Because Perl arrays have zero-based indexing, $[ will—almost always—be 0. But if you set $[ to 1, all your arrays will use one-based indexing. Don’t do it—$[ may disappear in future versions of Perl. It won’t be used in any of the programs in this book.

Negative Array Indexes

You can access the “nth to last” element of an array with negative array indexes.

$movies[-1]

is the last element of @movies. And

$movies[-3]

is the third-to-last element of @movies.

$movies[-10] = ‘The Adventures of Buckaroo Banzai’;

sets the tenth-to-last element of @movies to The Adventures of Buckaroo Banzai.

Adding and Removing Array Elements

Perl supplies several functions for moving array elements around. We’ll discuss five now: push(), pop(), shift(), unshift(), and splice().

push() and pop()

push() and pop() manipulate individual elements on the end (the right side) of an array.


The push() and pop() Functions
push (ARRAY, LIST) appends the LIST of elements to the end of an array (Figure 1-8).
pop (ARRAY) removes and returns the last ELEMENT of an array.


Figure 1-8  push() in action

pop() is to arrays as chop() is to strings—they both lop off and return the last item.

unshift() and shift()

unshift() and shift() are similar to push() and pop(), but operate on the beginning of the array instead of the end.


The unshift() and shift() Functions
unshift (ARRAY, LIST) the LIST of elements to the beginning of ARRAY.
shift (ARRAY) removes and returns the first element of ARRAY.

Let’s say you want to remove a movie from @movies. Here are a few ways to do it:

pop(@movies);                   # Removes the last element of @movies
shift(@movies);                 # Removes the first element of @movies

pop() and shift() return the element that was removed from the array.

$last  = pop(@movies);          # Removes the last element of @movies,
                                #      placing it in $last

$first = shift(@movies);        # Removes the first element of @movies,
                                #      placing it in $first

To add an element to @movies, you can use either push() or unshift().

push (@movies, ‘Ben Hur’);      # Adds ‘Ben Hur’ to the end of @movies

unshift(@movies, ‘Ben Hur’);    # Adds ‘Ben Hur’ to the beginning of
                                  @movies

splice()

To access (or modify) a chunk of an array, you can use splice().


The splice() Function
splice(ARRAY, OFFSET, LENGTH, LIST) removes and returns LENGTH elements from ARRAY (starting at index OFFSET), replacing them with LIST.

You can omit the LENGTH or LIST arguments, as shown.

splice(@movies, 4)

removes and returns all elements after (and including) $movies[4].

splice(@movies, 4, 2)

removes and returns $movies[4] and $movies[5].

splice(@movies, 4, 2, ‘UHF’, ‘Under Siege’)

removes and returns $movies[4] and $movies[5], replacing them with UHF and Under Siege.

But you don’t need splice() all that often because you can access array values without any functions at all.

@array[3, 5, 7] = (“hi”, 7.5, “there”)

sets $array[3] to hi, $array[5] to 7.5, and $array[7] to there, as does

($array[3], $array[5], $array[7]) = (“hi”, 7.5, “there”)

whereas

@array[3..5] = (“hi”, 7.5, “there”)

sets $array[3] to hi, $array[4] to 7.5, and $array[5] to there, which is equivalent to

splice(@array, 3, 3, “hi”, 7.5, “there”)

Quiz 5

1.  Which of the following statements is false?
a.  @movies is an array.
b.  $movies[3] is the fourth element of the array @movies.
c.  @movies[1] is a scalar.
d.  @movies[1..2] is an array.
2.  What’s $movie?
@movies = (‘Casablanca’, ‘Star Wars’, ‘E.T.’, ‘Home Alone’);

$movie = pop(@movies);
push(@movies, $movie);
$movie = pop(@movies);
$movie = pop(@movies);
push(@movies, $movie);
$movie = pop(@movies);
a.  Casablanca
b.  Star Wars
c.  E.T.
d.  Home Alone
3.  What does @letters contain?
@letters = ();
$letters[0] = ‘c’;
$letters[1] = ‘o’;
$letters[2] = ‘d’;
$letters[0] = ‘h’;
$letters[3] = ‘e’;
@letters[1..2] = (‘a’, ‘r’);
push(@letters, ‘s’);
a.  hares
b.  core
c.  share
d.  cares
4.  (Beware! This one is tricky.) Suppose @blort is an array of numbers. What describes the behavior of the following?
push(@blort, shift(@blort))
a.  The last number is moved to the beginning.
b.  Both the first and last numbers are removed.
c.  The first number is moved to the end.
d.  The first and last numbers are swapped.


Previous Table of Contents Next