Perl pop Function

Perl pop function is used to remove and return the last element of an array and is analogous with the strings chop function that removes the last character of a string. The size of the array will be shortened by 1. For arrays, it is similar with shift function that removes the first element of an array and is opposite to push/unshift function that adds a list at the end/front of an array.

Please note that whereas push function generally appends a list at the end of an array, pop function removes only the last element of the array, and not a list of elements.

You can manipulate an array like a:

  • stack by using together pop and push functions
  • queue by using both pop and unshift functions
The next short free tutorial will show you by some examples how you can use the Perl pop function in your scripts.

Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!

This function has two syntax forms:

pop (ARRAY)

If you use the first syntax form and there are no elements in the array, it returns the undef value. The parentheses can be omitted. If you use the second form where the array is omitted, then Perl pop function will be applied against some special arrays:
  • @ARGV array in the main program
  • @_ array if you use it within the lexical scope of a subroutine.
The following code snippet will show you a simple example about how you can use it:


use strict;
use warnings;

# initialize an array
my @numbers = qw(one two three four);
my $number = pop(@numbers);
# or my $number = $numbers[$#numbers-1];
# $#numbers--;
print "Removed: $number, \@numbers: @numbers\n";
# it displays Removed: four, @numbers: one two three

Applying Perl pop function has a similar effect as the two code lines:

my $number = $numbers[$#numbers-1];

where the last element of the array is stored in the scalar variable $number and next the size of the array is shortened by 1.

Using pop with a stack array

You can treat the "right side" of the array as the top of the stack and use pop and push functions to add and remove elements onto the end of the array. Please look at the following code snippet:


use strict;
use warnings;

# initialize th stack array with some operators
my @stack = qw(+ - * / %);
# remove the last element
pop @stack;
print "@stack\n";
# stack is now (+ - * /) 
# append an element
push(@stack, '>');
print "@stack\n";
# stack is now (+ - * / >)

In the same way you can treat the "left side" of the array as the top of the stack, using shift & unshift functions.

Using pop with a queue array

An array can be also thought as a queue. We can manipulate the array as a backward queue by using unshift function to enqueue in front of the array and pop to dequeue onto the end of the array.


use strict;
use warnings;

# initialize an array
my @queue = qw(if while switch for foreach);

# implement a backward queue array
pop (@queue);
unshift @queue, 'unless';
print "@queue\n";
# it displays: unless if while switch for

In a similar way, you can manipulate an array as a forward queue by using push (to enqueue at the end of the array) and shift (to dequeue at the front of the array).

Emulate a circular list with pop

A simple circular list can be thought as a queue and we can use pop and unshift functions to provide some functionality to it. Look at the following example:


use strict;
use warnings;

my @circularList = ('a'.. 'f');

unshift(@circularList, pop(@circularList));
print "\@circularList  = @circularList\n";
# it expects: @circularList = f a b c d e

The Perl pop function will return and remove the last element of the circular list, i.e. 'f', and the unshift function will insert this element in front of the circular list. The elements of the circular list will move from left to right.

Please click here to download the Perl pop script with all the above examples included.


Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl pop function. These exercises are completely covered in my Perl Functions for Real Arrays where I show you how to play with this important function in detail.

1. Write a single line of code where you print an array using pop within a while loop.
2. Give an example how to use pop with a list.
3. Show an example where to use pop with split.
4. Reverse the following array: @array = 1..10 using pop and push.
5. Given the following array of arrays:
my @AoA = ( 
  		[ 'a1', 'b1', 1 ],
  		[ 'a2', 'b2', 2 ],
  		[ 'a3', 'b3', 3 ],
write a short code to delete the last element from each subarray of the @AoA. If you print the output array, you need to get:
a1 b1
a2 b2
a3 b3
6. For the same array as before, remove the last element of the @AoA array and the sub-array referenced by this element. Print the @AoA array and the removed element (saved in $ref). The output must be:
a1 b1 1
a2 b2 2
$ref = a3 b3 3
7. Given the following array of hashes:
my @AoH = ( 
  	{ name => 'John', age => '21' },
  	{ name => 'Paul', age => '35' },
  	{ name => 'Mary', age => '50' }, 
remove the last element of the array of hashes. Next, print the @AoH as:
John is 21 years old.
Paul is 35 years old.
8. Let’s say you have the following hash of arrays:
my %HoA = (
      	colors => ['blue', 'white', 'red', 'green', 'yellow'],
      	shapes => ['square', 'triangle', 'circle', 'rectangle'],
Use pop against each inner array of the %HoA hash of arrays. Print the resulting hash as:
shapes => square triangle circle
colors => blue white red green
9. Give an example where to use pop within the body of a subroutine (to take and return the subroutine arguments).
10. Use pop and @_ to create a hash inside a subroutine and return it as a hash reference.
11. Give an example where you use pop with @ARGV.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!

Check these how-to tutorial eBooks (PDF format):

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        pop (more)

return from Perl pop Function to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!