Perl 5 arrays are data structures which store zero or more scalars. They're first-class data structures, which means that Perl 5 provides a separate data type at the language level. Arrays support indexed access; that is, you can access individual members of the array by integer indexes.
The @
sigil denotes an array. To declare an array:
Accessing an individual element of an array in Perl 5 requires the scalar sigil. Perl 5 (and you) can recognize that $cats[0]
refers to the @cats
array even despite the change of sigil because the square brackets ([]
) always identify indexed access to an aggregate variable. In simpler terms, that means "look up one thing in a group of things by an integer".
The first element of an array is at index zero:
The last index of an array depends on the number of elements in the array. An array in scalar context (due to scalar assignment, string concatenation, addition, or boolean context) evaluates to the number of elements contained in the array:
If you need the specific index of the final element of an array, subtract one from the number of elements of the array (because array indexes start at 0):
You can also use the special variable form of the array to find the last index; replace the @
array sigil with the slightly more unwieldy $#
:
That may not read as nicely, however. Most of the time you don't need that syntax, as you can use negative offsets to access an array from the end instead of the start. The last element of an array is available at the index -1
. The second to last element of the array is available at index -2
, and so on. For example:
You can resize an array by assigning to $#
. If you shrink an array, Perl will discard values which do not fit in the resized array. If you expand an array, Perl will fill in the expanded values with undef
.
You can assign to individual positions in an array directly by index:
Perl 5 arrays are mutable. They do not have a static size; they expand or contract as necessary.
Assignment in multiple lines can be tedious. You can initialize an array from a list in one step:
Any expression which produces a list in list context can assign to an array:
Assigning to a scalar element of an array imposes scalar context, while assigning to the array as a whole imposes list context.
To clear an array, assign an empty list:
You can also access elements of an array in list context with a construct known as an array slice. Unlike scalar access of an array element, this indexing operation takes a list of indices and uses the array sigil (@
):
You can assign to an array slice as well:
A slice can contain zero or more elements--including one:
The only syntactic difference between an array slice of one element and the scalar access of an array element is the leading sigil. The semantic difference is greater: an array slice always imposes list context. Any array slice evaluated in scalar context will produce a warning:
An array slice imposes list context (context_philosophy) on the expression used as its index:
Managing array indices can be a hassle. Because Perl 5 can expand or contract arrays as necessary, the language also provides several operations to treat arrays as stacks, queues, and the like.
The push
and pop
operators add and remove elements from the tail of the array, respectively:
You may push
as many elements as you like onto an array. Its second argument is a list of values. You may only pop
one argument at a time. push
returns the updated number of elements in the array. pop
returns the removed element.
Similarly, unshift
and shift
add elements to and remove an element from the start of an array:
unshift
prepends a list of zero or more elements to the start of the array and returns the new number of elements in the array. shift
removes and returns the first element of the array.
splice
is another important--if less frequently used--array operator. It removes and replaces elements from an array given an offset, a length of a list slice, and replacements. Both replacing and removing are optional; you may omit either behavior. The perlfunc
description of splice
demonstrates its equivalences with push
, pop
, shift
, and unshift
.
Arrays often contain elements to process in a loop (looping_directives).
As of Perl 5.12, you can use each
to iterate over an array by index and value:
In list context, arrays flatten into lists. If you pass multiple arrays to a normal Perl 5 function, they will flatten into a single list:
Within the function, @_
will contain seven elements, not two. Similarly, list assignment to arrays is greedy. An array will consume as many elements from the list as possible. After the assignment, @cats
will contain every argument passed to the function. @dogs
will be empty.
This flattening behavior sometimes confuses novices who attempt to create nested arrays in Perl 5:
While some people may initially expect this code to produce an array where the first ten elements are the numbers one through ten and the eleventh element is an array containing the numbers eleven through 20 and an array containing the numbers twenty-one through thirty, this code instead produces an array containing the numbers one through 30, inclusive. Remember that parentheses do not create lists in these circumstances--they only group expressions.
The solution to this flattening behavior is the same for passing arrays to functions and for creating nested arrays (array_references).
Arrays interpolate in double quoted strings as a list of the stringification of each item separated by the current value of the magic global $"
. The default value of this variable is a single space. Its English.pm mnemonic is $LIST_SEPARATOR
. Thus:
Temporarily localizing and assigning another value to $"
for debugging purposes is very handyDue credit goes to Mark-Jason Dominus for demonstrating this example several years ago.:
... which produces the result:
Hey! The above document had some coding errors, which are explained below:
- Around line 3:
-
A non-empty Z<>
- Around line 399:
-
Deleting unknown formatting code N<>