Perl Variables  «Prev 

Using an array as a stack in Perl

Stack push

Diagram of push to the stack
When you push a new element onto the stack, it goes on at the top, as the new highest-numbered element.

A stack is much like a queue except that you remove the most recently added element rather than the least recently added. The FIFO order has been changed to LIFO (last-in first-out). A typical example (which is why the data structure has this name) is a stack of plates in a cafeteria:
Diners take the top plate from the stack, but when a new plate has been washed, it is put on top of the stack and will be used next.

Stacks are frequently used when operations need to be broken down into suboperations to be executed in sequence. When such a compound operation is encountered, the operation is popped off, and the suboperations are pushed ontocontinue


One of the most basic uses for an array is as a stack. If you consider that an array is a list of individual scalars, it should be possible to treat it as if it were a stack of papers. Index 0 of the array is the bottom of the stack, and the last element is the top. You can put new pieces of paper on the top of the stack (push), or put them at the bottom (unshift). You can also take papers off the top (pop) or bottom (shift) of the stack.
There are, in fact, four different types of stacks that you can implement. By using different combinations of the Perl functions, you can achieve all the different combinations of LIFO, FIFO, FILO, and LILO stacks, as shown in Table 2-10.

Table 2-10: Stack Types and Functions

Acronym Description Function Combination
LIFO Last in, first out push/shift
LIFO First in, first out unshift/shift
FILO First in, last out unshift/pop
FILO Last in, last out push/pop

Like its cousin pop, if ARRAY is not specified, it shifts the first value from the @_ array within a subroutine, or the first command line argument stored in @ARGV otherwise. The opposite is unshift, which places new elements at the start of the array:
unshift ARRAY, LIST

This places the elements from LIST, in order, at the beginning of ARRAY. Note that the elements are inserted strictly in order, such that the code
unshift @array, 'Bob', 'Phil';

will insert "Bob" at index 0 and "Phil" at index 1.

shift and unshift

Note that shift and unshift will affect the sequence of the array more significantly (because the elements are taken from the first rather than last index). Therefore, care should be taken when using this pair of functions. However, the shift function is also the most practical when it comes to individually selecting the elements from a list or array, particularly the @ARGV and @_ arrays. This is because it removes elements in sequence: the first call to shift takes element 0, the next takes what was element 1, and so forth. The unshift function also has the advantage that it inserts new elements into the array at the start, which can allow you to prepopulate arrays and lists before the information provided. This can be used to insert default options into the @ARGV array, for example.