| Previous | Table of Contents | Next |
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. (Theres a subtle difference between existing and being defined, explained further in Chapter 5, Session 5.)
$[ is a special variable. Youll 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, $[ willalmost alwaysbe 0. But if you set $[ to 1, all your arrays will use one-based indexing. Dont do it$[ may disappear in future versions of Perl. It wont be used in any of the programs in this book.
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.
Perl supplies several functions for moving array elements around. Well discuss five now: push(), pop(), shift(), unshift(), and splice().
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 stringsthey both lop off and return the last item.
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.
Lets 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
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 dont 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)
@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);
@letters = (); $letters[0] = c; $letters[1] = o; $letters[2] = d; $letters[0] = h; $letters[3] = e; @letters[1..2] = (a, r); push(@letters, s);
push(@blort, shift(@blort))
| Previous | Table of Contents | Next |