Chapter 21. Array and Hash Functions

defined expr

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

delete lku

delete @array[index1, . . . ]

delete @hash{key1, key2, . . . }

lku must be an array lookup like $array[index] or expr->[index], or a hash key lookup like $hash{key} or expr->{key}. Deletes the specified elements from the array or hash. Returns aliases to the deleted values. Deleting the last element(s) of an array will shorten the array.

each %hash

In list context, returns a two-element list consisting of the key and an alias to the value for the next element of the hash. In scalar context, returns only the key. Entries are returned in an apparently random order. After all values of the hash 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 lku

lku must be an array a hash lookup (see delete above). Checks whether the specified array element or hash key 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 %hash

In list context, returns a list of all the keys of the named hash. In scalar context, returns the number of elements of the hash. Can be assigned to, to preextend the hash.

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.

scalar %hash

Returns true if there are keys in the hash.

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 removed. If offset is negative, counts from 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 %hash

Returns a list consisting of aliases to all the values of the named hash.

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

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