List-Manipulation Functions
There are two groups of functions that manipulate lists and arrays
The following functions manipulate standard array variables and the lists that they store
grep | extract the elements of a list that match a specified pattern |
splice | enables you to modify the list stored in an array variable, by passing appropriate arguments to splice, you can add elements to the middle of a list, delete portion of a list, or replace a portion of a list. |
shift | remove an item from the beginning a list |
unshift | put an item at the beginning of a list |
push | put an item to the end of a list |
pop | remove an item from the end of a list |
split | split a character string into a list of elements |
sort/reverse | sort a list alphabetically |
reverse | reverse the order of a list |
map | enables you to use each element of a list, in turn as an operand in an expression |
wantarray | wantarray determines if the calling function wants a scalar or array variable returned |
Examples |
|
grep | # grep (<pattern>, <searchlist>) $line = "This line of input contains 8, 11 and 26."; @numbers = grep(/^\d+[.,;:]?$/, @words); Note: you can also use grep with the file-test operators |
splice | # retval = splice(<array>, <skipelements>, <length>, <newlist>); # Replace # Appending # Deleting |
shift/unshift | ## add or delete from the left side of the array, element 0 @array = qw(1 2 3 4); $first_element = shift(@array); print ("Array: @array"); unshift(@array, $first_element); |
push/pop | ## add or delete from the right side of the array, last element @array = qw(1 2 3 4); $first_element = pop(@array); print ("Array: @array"); push(@array, $first_element); |
split | $string = "one::two::three::four::five::six"; |
sort | @array = qw( one two three four five six);
@sorted = sort(@array); |
reverse | @array = qw( one two three four five six);
@reversed = reverse(@array); |
map | # map(<expression>, <array>); # The map function uses the system variable $_ for each element @array = qw(100 200 300 400); @results = map($_+1, @array); print("@results"); |
wantarray | @array = &mysub(); # using an array the wantarray will return true $scalar = &mysub(); # using an scalar the wantarray will return false sub mysub { |
Here are some equivalent comparisions using the splice command assuming ($[ == 0 and $#a >= $i
)
Add an item on the end of a list | push(@a,$x) | splice(@a,@a,0,$x) |
Remove an item from the end of a list | pop(@a) | splice(@a,-1) |
Remove an item from the beginning of a list | shift(@a) | splice(@a,0,1) |
Add an item to the beginning of a list | unshift(@a,$x,$y) | splice(@a,0,0,$x,$y) |
set a element array to a value | $a[$x] = $y | splice(@a,$x,1,$y) |
To create a queue you would use push and shift, and to create stack you would use push and pop.
The following functions manipulate associative arrays
keys | returns a list of subscripts of the element of an associative array |
values | returns a list consisting of all the values in an associate array |
each | returns an associative element as a two element list |
delete | deletes an associative array element |
exists | enables you to determine whether a particular element of an associative array exists. |
Examples |
|
keys | %array = ("foo", "26", "bar", "27"); # keys is commonly used as below Note: in no particular order will the list be returned |
values | %array = ("foo", "26", "bar", "27"); @values = values(%array); print("@values\n"); foreach $i (values (%array)) { Note: in no particular order will the list be returned |
each | %array = ("foo", "26", "bar", "27"); @each = each(%array); print("@each\n"); foreach $i (each (%array)) { Note: in no particular order will the list be returned, also do not use delete when using each, because the behavior of each unpredictable |
delete | %array = ("foo", "26", "bar", "27");
$retval = delete($array{"foo"}); # returns the deleted elements value 26 foreach $i (keys (%array)) { print $retval; |
exists | %array = ("foo", "26", "bar", "27");
if ( exists($array{"foo"}) ) { |
When using associative arrays do not use push, pop, shift or splice because the position of any particular element in the array is not guaranteed.