Home
Perl How To
Perl Books
Perl Basics
Perl Editors
Perl Modules
Perl Database
Net Programming
Sitemap
About Me
Contact Me
Blog
 

Perl pop Function



NEW!!!

Check my new resource:    Perl How To Tutorial eBooks



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.

This function has two syntax forms:

pop (ARRAY)
pop

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 snippet code will show you a simple example about how you can use it:
#!/usr/bin/perl

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];
$#numbers--;

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 snippet code:

#!/usr/bin/perl

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.

#!/usr/bin/perl

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:

#!/usr/bin/perl

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.




NEW!!!

Do you want more information about the basic Perl topics?

Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :


Perl How To Tutorial eBooks


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
        Array Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        grep
        hex
        index
        join
        lc
        lcfirst
        length
        oct
        ord
        pack
        pop
        push
        reverse
        rindex
        shift
        split
        substr
        uc
        undef
        unshift

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!


footer for perl pop page