Array and List Functions

defined expr

Not specifically an array function, but provides a convenient way to test whether an array element has a defined value.

delete [ local ] elt

delete [ local ] @array[index1, . . . ]

elt must specify an array element like $array[index] or expr->[index]. Deletes the specified elements from the array. With local, the deletion is local to the enclosing block. Returns aliases to the deleted values. Deleting the last elements of an array will shorten the array.

each @array

In list context, returns a two-element list consisting of the index and an alias to the value for the next element of the array. In scalar context, returns only the index. After all values have been returned, an empty list is returned. The next call to each after that will start iterating again. A call to keys or values will reset the iteration.

exists elt

elt must specify an array element (see delete above). Checks whether the specified array element exists.

grep expr, list

grep block list

Evaluates expr or block for each element of the list, locally aliasing $_ to the element. In list context, returns the list of elements from list for which expr or block returned true. In scalar context, returns the number of such elements.

join expr, list

Returns the string formed by inserting expr between all elements of list and concatenating the result.

keys @array

In list context, returns a list of all the keys of the array. In scalar context, returns the number of elements.

map expr, list

map block list

Evaluates expr or block for each element of the list, locally aliasing $_ to the element. Returns the list of results.

pop [ @array ]

Pops off and returns the last value of the array. If @array is omitted, pops @_ if inside a subroutine; otherwise pops @ARGV.

push @array, list

Pushes the values of the list onto the end of the array. Returns the length of the resulting array.

reverse list

In list context, returns the list in reverse order. In scalar context, concatenates the list elements and returns the reverse of the resulting string.

scalar @array

Returns the number of elements in the array.

shift [ @array ]

Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If @array is omitted, shifts @_ if inside a subroutine; otherwise shifts @ARGV.

sort [ subroutine ] list

Sorts the list and returns the sorted list value. subroutine, if specified, must return less than zero, zero, or greater than zero, depending on how the elements of the list are to be ordered.

subroutine may be the name of a user-defined routine, a variable containing that name, or a block. If the subroutine has been declared with a prototype of ($$), the values to be compared are passed as normal parameters; otherwise, they are available to the routine as package global variables $a and $b.

splice @array, offset [ , length [ , list ] ]

Removes the elements of @array designated by offset and length, and replaces them with list (if specified). Returns the elements that were removed. If offset is negative, counts from the end of the array. If length is negative, leaves elements at the end of the array.

split [ pattern [ , expr† [ , limit ] ] ]

Uses pattern to split expr (a string) into a list of strings, and returns it. If limit is a positive number, splits into at most that number of fields. A negative value indicates the maximum number of fields. If limit is omitted, or 0, trailing empty fields are not returned. If pattern is omitted, splits at the whitespace (after skipping any leading whitespace). If not in list context, returns number of fields and splits to @_.

unshift @array, list

Prepends list to the front of the array. Returns the length of the resultant array.

values @array

Returns a list consisting of aliases to all the values of the array.

each, keys, pop, push, shift, splice, unshift, and values may take an array reference as the first argument. Experimental.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset