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.
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
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
Perl 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";
}
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.
Using the Perl Splice Function: A Comprehensive Guide
At our company, we have extensive experience in programming with Perl, and we believe that the splice function is one of the most powerful tools in the Perl arsenal. This article will provide a comprehensive guide to the use cases for the Perl splice function, and demonstrate how it can be used to manipulate arrays in various ways.
What is the Perl splice function?
The splice function is a built-in Perl function that allows you to remove or replace elements from an array, and optionally insert new elements in their place. The syntax of the function is as follows:
splice ARRAY, OFFSET, LENGTH, LIST
ARRAY: the array that you want to manipulate
OFFSET: the index of the element where the manipulation should start
LENGTH: the number of elements that should be removed from the array
LIST: a list of elements that should be inserted into the array
Removing Elements from an Array:
The most basic use case for the splice function is to remove one or more elements from an array. To remove a single element, you would use the following code:
my @array = qw(a b c d e);
splice(@array, 2, 1);
This code removes the element at index 2 (the third element, 'c') from the array. The resulting array would be ('a', 'b', 'd', 'e'). To remove multiple elements from the array, you can simply increase the value of the LENGTH parameter. For example, to remove the elements at indices 1 and 2, you would use the following code:
my @array = qw(a b c d e);
splice(@array, 1, 2);
This code removes the elements at indices 1 and 2 ('b' and 'c') from the array. The resulting array would be ('a', 'd', 'e').
Replacing Elements in an Array
Another common use case for the splice function is to replace one or more elements in an array with new elements. To replace a single element, you would use the following code:
my @array = qw(a b c d e);
splice(@array, 2, 1, 'x')
This code replaces the element at index 2 (the third element, 'c') with the value 'x'. The resulting array would be ('a', 'b', 'x', 'd', 'e').
To replace multiple elements in the array, you can simply increase the value of the LENGTH parameter, and provide a list of replacement elements in the LIST parameter. For example, to replace the elements at indices 1 and 2 with 'x' and 'y', you would use the following code:
my @array = qw(a b c d e);
splice(@array, 1, 2, 'x', 'y');
This code replaces the elements at indices 1 and 2 ('b' and 'c') with the values 'x' and 'y'. The resulting array would be ('a', 'x', 'y', 'd', 'e').
Inserting Elements into an Array:
The final use case for the splice function is to insert new elements into an array, without removing any existing elements. To insert a single element, you would use the following code:
my @array = qw(a b c d e);
splice(@array, 2, 0, 'x');
This code inserts the value 'x' at index 2 (between 'b' and 'c'). The resulting array would be `
Perl 'Splice' function
One final function that operates on arrays is the splice function. This function is used to extract a chunk of data from the middle of an array.
Note: It is important to note that after the splice operation, the original array no longer contains the elements spliced out. For example, consider the following code:
The splice operation started at element number three and removed four elements. So elements three, four, five, and six have been removed from the array. The splice function is not used as frequently as the others, but it is good to have an understanding of how it works.
Removes the elements of ARRAY from the element OFFSET for LENGTH elements, replacing the elements removed with LIST, if specified. If LENGTH is omitted, removes everything from OFFSET onwards.
Effects
$@
Returns in Scalar Context
Returns in List Context
undef if no elements removed
Empty list on failure
Last element removed
List of elements removed
Push Function - Exercise
Click the Exercise link below to write a program that uses the push function. Push Function Exercise