next up previous contents
Next: Associative Arrays Up: Arrays Previous: Indexed Arrays

Some Useful Array Functions

push() and pop()

One common use of an array is as a stack.

push() and pop() add or remove an item from the right hand side of an array.

# add new value to stack

$off_value = pop(@mystack); 
# take last element off array

shift() and unshift()

Like push() and pop() except put values on and take values off the left side of an array.
The shift() operation 'pops' the element from the left side of the array and unshift() 'pushes' to the left side of an array.


As one would expect this will reverse the ordering of list. For example:

@a = (1,2,3);

@b = reverse(@a);

results in b containing (3,2,1).


This is a useful function that sorts an array. NOTE: Sorting is done on the string values of each number (alphabetical)


@x = sort("small","medium","large");

# gets @x = ("large","medium","small");

@y = sort(1,,32,16,4,2);

# gets @x = (1,16,2,32,4);


Just as on a scalar string it removes the last element from an array, for example:

@x = ("small","medium","large");


# gets @x =  ("small","medium")