Probably the most important array operator is the array assignment operator, which gives an array variable a value. It is an equal sign, just like the scalar assignment operator. Perl determines whether the assignment is a scalar assignment or an array assignment by noticing whether the assignment is to a scalar or an array variable. For example:
 This applies to scalar or array "lvalues" as well as to simple variables.
@fred = (1,2,3); # The fred array gets a three-element literal @barney = @fred; # now that is copied to @barney
@huh = 1; # 1 is promoted to the list (1) automatically # that is, @huh now is (1)
@fred = qw(one two); @barney = (4,5,@fred,6,7); # @barney becomes # (4,5,"one","two",6,7) @barney = (8,@barney); # puts 8 in front of @barney @barney = (@barney,"last"); # and a "last" at the end # @barney is now (8,4,5,"one","two",6,7,"last")
Note that the inserted array elements are at the same level as the rest of the literals: a list cannot contain another list as an element.
If a list literal contains only variable references (not expressions), you can treat the list literal as a variable. In other words, you can use it on the left side of an assignment. Each scalar variable in the list literal takes on the corresponding value from the list on the right side of the assignment. For example:
($a,$b,$c) = (1,2,3); # give 1 to $a, 2 to $b, 3 to $c ($a,$b) = ($b,$a); # swap $a and $b ($d,@fred) = ($a,$b,$c); # give $a to $d, and ($b,$c) to @fred ($e,@fred) = @fred; # remove the first element of @fred to $e # this makes @fred = ($c) and $e = $b
If the number of elements you assign does not match the number of variables to hold the values, any excess values (on the right side of the equal sign) are silently discarded, and any excess variables (on the left side of the equal sign) are given the value of
An array variable appearing in the array literal list must be last, because the array variable is "greedy" and consumes all remaining values. (Well, you could put other variables after it, but they would just get
@fred = (4,5,6); # initialize @fred $a = @fred; # $a gets 3, the current length of @fred
The length is also returned whenever you use an array variable name where a scalar value is needed. (In the section "Scalar and List Context" later in this chapter, we'll see that this method is called using the array name in a scalar context.) For example, to get one less than the length of the array, you can use
@fred-1, because the scalar subtraction operator wants scalars for both of its operands. Notice the following:
$a = @fred; # $a gets the length of @fred ($a) = @fred; # $a gets the first element of @fred
The first assignment is a scalar assignment, and so
@fred is treated as a scalar, yielding its length. The second assignment is an array assignment (even if only one value is wanted), and thus yields the first element of
@fred, silently discarding all the rest.
The value of an array assignment is itself a list value, and can be cascaded as with scalar assignments. For example:
@fred = (@barney = (2,3,4)); # @fred and @barney get (2,3,4) @fred = @barney = (2,3,4); # same thing
So far, we've been treating the array as a whole, adding and removing values by doing array assignments. Many useful programs are constructed using arrays without ever accessing any specific array element. However, Perl provides a traditional subscripting operator to access an array element by numeric index.
For the subscripting operator, the array elements are numbered using sequential integers, beginning at 0, and increasing by 1 for each element. The first element of the
@fred array is accessed as
$fred. Note that the
@ on the array name becomes a
$ on the element reference. This is because accessing an element of the array identifies a scalar variable (part of the array), which can either be assigned to or have its current value used in an expression, like so:
 You can change the index value of the first element to something else (like 1). However, doing so has drastic effects, will probably confuse people maintaining your code, and might break the routines you take from other people. Thus, we highly recommend that you consider this feature unusable.
@fred = (7,8,9); $b = $fred; # give 7 to $b (first element of @fred) $fred = 5; # now @fred = (5,8,9)
Other elements can be accessed with equal ease, as in:
$c = $fred; # give 8 to $c $fred++; # increment the third element of @fred $fred += 4; # add 4 to the second element ($fred,$fred) = ($fred,$fred); # swap the first two
@fred[0,1] # same as ($fred,$fred) @fred[0,1] = @fred[1,0] # swap the first two elements @fred[0,1,2] = @fred[1,1,1] # make all 3 elements like the 2nd @fred[1,2] = (9,10); # change the last two values to 9 and 10
Note that this slice uses an
@ prefix rather than a
$. This is because you are creating an array variable by selecting part of the array rather than a scalar variable accessing just one element.
@who = (qw(fred barney betty wilma))[2,3]; # like @x = qw(fred barney betty wilma); @who = @x[2,3];
The index values in these examples have been literal integers, but the index can also be any expression that returns a number, which is then used to select the appropriate element:
@fred = (7,8,9); $a = 2; $b = $fred[$a]; # like $fred, or the value of 9 $c = $fred[$a-1]; # $c gets $fred, or 8 ($c) = (7,8,9)[$a-1]; # same thing using slice
Perl programs can thus have array accesses similar to many traditional programming languages.
This idea of using an expression for the subscript also works for slices. Remember, however, that the subscript for a slice is a list of values, so the expression is an array expression, rather than a scalar expression. For example:
@fred = (7,8,9); # as in previous example @barney = (2,1,0); @backfred = @fred[@barney]; # same as @fred[2,1,0], or ($fred,$fred,$fred), or # (9,8,7)
If you access an array element beyond the ends of the current array (that is, an index of less than 0 or greater than the last element's index), the
undef value is returned without warning. For example:
@fred = (1,2,3); $barney = $fred; # $barney is now undef
@fred = (1,2,3); $fred = "hi"; # @fred is now (1,2,3,"hi") $fred = "ho"; # @fred is now (1,2,3,"hi",undef,undef,"ho")
You can use
$#fred to get the index value of the last element of
@fred. You can even assign to this value to change the apparent length of
@fred, making it grow or shrink, but such an assignment is generally unnecessary, because the array grows and shrinks automatically.
A negative subscript on an array counts back from the end. So, another way to get at the last element is with the subscript -1. The second to the last element would be -2, and so on. For example:
@fred = ("fred", "wilma", "pebbles", "dino"); print $fred[-1]; # prints "dino" print $#fred; # prints 3 print $fred[$#fred]; # prints "dino"
One common use of an array is as a stack of information, where new values are added to and removed from the right-hand side of the list. These operations occur often enough to have their own special functions:
push(@mylist,$newvalue); # like @mylist = (@mylist,$newvalue) $oldvalue = pop(@mylist); # removes the last element of @mylist
push() function also accepts a list of values to be pushed. The values are pushed together onto the end of the list. For example:
@mylist = (1,2,3); push(@mylist,4,5,6); # @mylist = (1,2,3,4,5,6)
Note that the first argument must be an array variable name - pushing and popping wouldn't make sense on a literal list.
pop functions do things to the "right" side of a list (the portion with the highest subscripts). Similarly, the
shift functions perform the corresponding actions on the "left" side of a list (the portion with the lowest subscripts). Here are a few examples:
unshift(@fred,$a); # like @fred = ($a,@fred); unshift(@fred,$a,$b,$c); # like @fred = ($a,$b,$c,@fred); $x = shift(@fred); # like ($x,@fred) = @fred; # with some real values @fred = (5,6,7); unshift(@fred,2,3,4); # @fred is now (2,3,4,5,6,7) $x = shift(@fred); # $x gets 2, @fred is now (3,4,5,6,7)
undef if you give it an empty array variable.
@a = (7,8,9); @b = reverse(@a); # gives @b the value of (9,8,7) @b = reverse(7,8,9); # same thing
Note that the argument list is unaltered; the
reverse() function works on a copy. If you want to reverse an array "in place," you'll need to assign it back into the same variable:
@b = reverse(@b); # give @b the reverse of itself
@x = sort("small","medium","large"); # @x gets "large","medium","small" @y = (1,2,4,8,16,32,64); @y = sort(@y); # @y gets 1,16,2,32,4,64,8
Note that sorting numbers does not happen numerically, but by the string values of each number (
32, and so on). In the section "Advanced Sorting" in Chapter 15, Other Data Transformation, you'll learn how to sort numerically, in descending order, by the third character of each string, or by any other method that you choose.
chomp function works on an array variable as well as a scalar variable. Each element of the array has its last newline removed. This function can be handy when you've read a list of lines as separate array elements, and you want to remove a newline from all of the lines at once. For example:
@stuff = ("hello\n","world\n","happy days"); chomp(@stuff); # @stuff is now ("hello","world","happy days")