Perl Variables  «Prev  Next»
Lesson 10Using an array as a stack
ObjectivePrompt String, push onto array

Perl Stack Array

Program that prompts for string, pushes it onto an array.
Using an array as a stack in Perl. The array structure in Perl can be used as more than just an accumulation of scalar values. It is also used as a stack, which is often a very convenient data abstraction.

Diagram of a stack
Diagram of a stack


Visualize a data stack as being like a stack of blocks. The block at the bottom of the stack is array element 0, and the block at the top of the stack is the highest-numbered element in the array. A stack is sometimes called a LIFO (last-in/first-out) data structure, because the most recently added data is always the first data retrieved.

Perl Push function

The Perl function push is used to push a new value onto an array. In the following example, the latest Academy Award-winner is added to the list:

#!/usr/bin/perl -w

@bestpix = ( "Ordinary People",
"Chariots of Fire", "Gandhi",
"Terms of Endearment", "Amadeus",
"Out of Africa", "Platoon",
"The Last Emperor", "Rain Man",
"Driving Miss Daisy",
"Dances with Wolves",
"The Silence of the Lambs", "Unforgiven",
"Schindler's List", "Forrest Gump",
"Braveheart", "The English Patient" ); 

>push @bestpix, "Titanic";
print join("\n", @bestpix);
print "\n";


Stack push

Diagram of push to the stack
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

Stacks

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.

Perl Pop function

When you pop an element from the stack, it is removed from the top in a last-in, first-out (LIFO) manner.
You can use Perl's pop function to remove elements from the stack in reverse order, like this:

while (@bestpix) {
  $pic = pop @bestpix; 
  print "$pic\n";
  }

Stack Pop Function
There are a few other lesser-used functions that work specifically on arrays:
The following two functions work specifically on arrays. First there is the shift and unshift, then there is the Perl Splice Function.

Push Function - Exercise

Click the Exercise link below to write a program that uses the push function.
  Push Function Exercise