How to pop elements from an array in Perl?

Question

How to pop elements from an array in Perl?

How to Pop Elements from an Array in Perl

In Perl, arrays are flexible ordered lists of scalars identified by the @ sigil. One of the common operations on arrays is removing elements from the end, and this is where the pop function becomes very useful.

The pop function removes and returns the last element of an array. Perl arrays use zero-based indexing, and pop always acts on the element at the highest index, shrinking the array by one element each time it is called. This makes it perfect for implementing stack-like behavior (Last In, First Out).

Basic Usage

The simplest form is:


my @array = ('a', 'b', 'c', 'd');
my $element = pop(@array);
print "Popped element: $element\n";          # prints: d
print "Remaining array: @array\n";           # prints: a b c

Here, pop removes 'd', the last element, from @array. The function returns the popped element, which you can capture in a scalar or ignore if you only want to modify the array.

Details and Explanation

  • Sigil Context: Notice pop expects an array (prefixed with @).
  • Return Value: Returns the popped element (scalar context).
  • Modifies Original Array: The array is modified in place, shortening its length.
  • Empty Array Behavior: If the array is empty, pop returns undef.
  • TMTOWTDI: While you can also manipulate arrays by slicing or splicing, pop is the idiomatic and clear way to remove from the end.

Multiple Pops in a Loop

You can repeatedly pop elements in a loop until the array is empty, for example:


my @values = (1, 2, 3);

while (defined(my $v = pop @values)) {
    print "Popped: $v\n";
}
print "Array is now empty. Length: ", scalar(@values), "\n";

Common Pitfalls

  • Forgetting the @ sigil: pop requires an array, so pop(@array) not pop($array).
  • Not Catching undef return: When popping from an empty array, you get undef—watch out for subtle bugs.
  • Accidentally popping from wrong array: When working with multiple arrays, ensure you pop from the intended one.

Complete Runnable Example


#!/usr/bin/perl
use strict;
use warnings;

my @fruits = qw(apple banana cherry date);
print "Original array: @fruits\n";

# Pop one element
my $last = pop @fruits;
print "Popped element: $last\n";
print "Array after one pop: @fruits\n";

# Pop all remaining elements
while (defined(my $fruit = pop @fruits)) {
    print "Popped in loop: $fruit\n";
}

print "Array length after popping all elements: ", scalar(@fruits), "\n";

This script prints each popped element and how the array shrinks until it is empty. It illustrates how pop works in scalar context and modifies the array in place.

In summary, pop is a fundamental Perl function for removing and retrieving the last element of an array. It is simple, efficient, and a core part of Perl’s rich set of array manipulation tools. Whenever you need to treat an array like a stack or remove elements from the end, pop is the go-to function.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 4ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Popped element: d
Remaining array: a b c
STDERR
(empty)

Was this helpful?

Related Questions