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.

push(@mystack,$newvalue); 
# 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.

reverse()

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).

sort()

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

Thus:

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

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

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

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

chop()

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

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

chop(@x);

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



dave@cs.cf.ac.uk