Syntax:
The syntax of the pop() function is given in the following. It returns the last element of the array if the array is non-empty. Otherwise, it returns “undef”.
Examples of Pop() Function
Different uses of the pop() function in Perl are shown in this part of the tutorial.
Example 1: Remove an Element from an Array of Strings
Create a Perl file with the following script that uses the pop() function to remove the last element of the array of four string values. In this script, four values of the array are initialized with four string values after declaring an empty array. Next, the pop() function is used to retrieve and remove the last value from the array. The array values are printed before and after removing the last element.
use strict;
use warnings;
use 5.34.0;
use Data::Dumper;
# Declare an empty array
my @str_array = ();
#Initialize four values of the array
$str_array[0] = 'Ubuntu';
$str_array[1] = 'Windows';
$str_array[2] = 'Fedora';
$str_array[3] = 'CentOS';
say "Array values are:";
#Print all values of the array using the dump variable
print Dumper (\@str_array);
#Remove the last value of the array
my $retrieved_value = pop(@str_array);
#Print the removed element of the array
print "\nThe removed value of the array is $retrieved_value.\n";
say "\nArray values after removing one element are:";
#Print all values of the array using the dump variable
print Dumper (\@str_array);
Output:
The following output appears after executing the script. The last value of the “CentOS” array is removed after executing the pop() function:
Example 2: Remove an Element from an Array of Numbers
Create a Perl file with the following script that uses the pop() function to remove the last two elements of the array of five numeric values. The “foreach” loop is used in the script to print the sum of all array values before and after removing the last two values. The pop() function is used two times in the script to remove two elements from the array.
use strict;
use warnings;
use 5.34.0;
no warnings 'uninitialized';
# Declare an array of numbers
my @num_array = (4, 8, 3, 9, 5);
#Initialize the sum variable
my $sum = 0;
my $len = scalar(@num_array);
#Calculate and print the sum of array values
foreach my $i (0..$len)
{
$sum = $sum + $num_array[$i];
}
say "The sum of array values is $sum";
#Remove the last two values of the array
print pop(@num_array)." is removed.\n";
print pop(@num_array)." is removed.\n";
foreach my $i (0..$len)
{
$sum = $sum + $num_array[$i];
}
#Re-initialize the variable
$sum = 0;
#Calculate and print the sum of array values after removing two values
foreach my $i (0..$len)
{
$sum = $sum + $num_array[$i];
}
say "The sum of array values after removing two values is $sum";
Output:
The following output appears after executing the script. The array values were 4, 8, 3, 9, and 5 before removing any element. The sum of these values is 29 which is printed. The last two values, 5 and 9, were removed using the pop() function. The sum of the remaining array values is 15 which is printed:
Example 3: Perform an Arithmetic Task Based on the Popped Array Value
Create a Perl file with the following script that takes the numeric values from the user and use the pop() function to retrieve the last value from an array that contains four arithmetic operators to perform the arithmetic tasks based on the input values. The “eval” function is in the script to get the result of the arithmetic task.
use strict;
use warnings;
use 5.34.0;
#Take the first number
print "Enter the first number: ";
my $number1 = <>;
chomp($number1);
#Take the second number
print "Enter the second number: ";
my $number2 = <>;
chomp($number2);
#Define operator array
my @opt = ('/', '*', '-', '+');
#Retrieve the last value of the array
my $operator = pop(@opt);
#Perform arithmetic operations based on the operator
my $result = eval "$number1 $operator $number2";
#Print the calculated result
say "$number1 $operator $number2 = $result";
Output:
The following output appears after executing the script if 5 and 3 are taken as the input values:
Conclusion
The uses of the pop() function to retrieve one or more values from a Perl array are shown in this tutorial using multiple examples.