Trusted answers to developer questions

How to pop an element or a value from an array in Perl

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Overview

In Perl, the pop() function is used to return the last value from an array. It removes the value from the array and returns it.

Syntax

pop(Array)
Syntax to pop an element from an array

Parameters

This function takes an array that we want to pop the last element or value of.

Return value

This function returns the element that was popped out from the array. This is the last element of the array.

Example

# create an array
@languages = ("C","Java", "Python", "Perl");
print "Before pop : \@coins = @languages\n";
# pop array
@popedElement = pop(@languages);
# print array after pop
print "After pop: \@gnaam = @languages\n";
# print popped value
print "Popped Value is @popedElement"

Explanation

  • Line 2: We create an array called languages.
  • Line 3: We print the values of the array before popping.
  • Line 5: We invoke the pop() function, which pops the last element of the array.
  • Line 7: We print the values of the array after popping.
  • Line 9: We print the popped value to the console.

RELATED TAGS

perl
array
pop
Did you find this helpful?