1. Home
  2. Computing & Technology
  3. Perl

Perl array pop() function - Quick Tutorial
How to use the array pop() function

From , former About.com Guide

$ITEM = pop(@ARRAY);
Perl's pop() function is used to remove and return (or pop) the last element from an array, which reduces the number of elements by one. The last element in the array is the one with the highest index. It's easy to confuse this function with shift(), which removes the first element from an array.
@myNames = ('Larry', 'Curly', 'Moe');
$oneName = pop(@myNames);
If you think of an array as a row of numbered boxes, going from left to right, it would be the element on the far right. The pop() function would cut the element off the right side of the array, return it, and reduce the elements by one. In the examples, the value of $oneName becomes 'Moe', the last element, and @myNames is shortened to ('Larry', 'Curly').

The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The pop() function would pop the element off the bottom of the stack, return it, and reduce the elements by one.

@myNames = (
'Larry',
'Curly',
'Moe'
);
$oneName = pop(@myNames);
Explore Perl
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Programming Perl
  5. Perl pop function reference - learn how to use Perl's pop() function in this quick tutorial.>

©2009 About.com, a part of The New York Times Company.

All rights reserved.