Skip to content

Latest commit

 

History

History
399 lines (260 loc) · 10.3 KB

arrays.pod

File metadata and controls

399 lines (260 loc) · 10.3 KB

Arrays

Perl 5 arrays are first-class data structures--the language supports them as a built-in data type--which store zero or more scalars. You can access individual members of the array by integer indexes, and you can add or remove elements at will. The @ sigil denotes an array. To declare an array:

Array Elements

Accessing an individual element of an array in Perl 5 requires the scalar sigil. $cats[0] is an unambiguous use of the @cats array, because postfix (fixity) square brackets ([]) always mean indexed access to an array.

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 in the array:

To get the index of the final element of an array, subtract one from the number of elements of the array (remember that array indexes start at 0) or use the unwieldy $#cats syntax:

When the index matters less than the position of an element, use negative array indices instead. 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:

$# has another use: resize an array in place by assigning to it. Remember that Perl 5 arrays are mutable. They expand or contract as necessary. When you shrink an array, Perl will discard values which do not fit in the resized array. When you expand an array, Perl will fill the expanded positions with undef.

Array Assignment

Assign to individual positions in an array directly by index:

If you assign to an index beyond the array's current bound, Perl will extend the array to account for the new size and will fill in all intermediary positions with undef. After the first assignment, the array will contain undef at positions 0, 1, and 2 and Jack at position 3.

As an assignment shortcut, initialize an array from a list:

... but remember that these parentheses do not create a list. Without parentheses, this would assign Daisy as the first and only element of the array, due to operator precedence (precedence).

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:

Array Operations

Sometimes an array is more convenient as an ordered, mutable collection of items than as a mapping of indices to values. Perl 5 provides several operations to manipulate array elements without using indices.

The push and pop operators add and remove elements from the tail of an array, respectively:

You may push a list of values onto an array, but you may only pop one at a time. push returns the new number of elements in the array. pop returns the removed element.

Because push operates on a list, you can easily append the elements of one or more arrays to another with:

Similarly, unshift and shift add elements to and remove an element from the start of an array, respectively:

unshift prepends a list of 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.

Few programs use the return values of push and unshift.

The splice operator 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. One effective use is removal of two elements from an array:

Prior to Perl 5.12, iterating over an array by index required a C-style loop. As of Perl 5.12, each can iterate over an array by index and value:

Array Slices

The array slice construct allows you to access elements of an array in list context. Unlike scalar access of an array element, this indexing operation takes a list of zero or more indices and uses the array sigil (@):

Array slices are useful for assignment:

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. An array slice evaluated in scalar context will produce a warning:

An array slice imposes list context on the expression used as its index:

Arrays and Context

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, because 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:

... but this code is effectively the same as:

... because these parentheses merely group expressions. They do not create lists in these circumstances. To avoid this flattening behavior, use array references (array_references).

Array Interpolation

Arrays interpolate in strings as lists of the stringifications 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:

Localize $" with a delimiter to ease your debuggingCredit goes to Mark Jason Dominus for this technique.:

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3:

A non-empty Z<>

Around line 257:

A non-empty Z<>

Around line 390:

Deleting unknown formatting code N<>