Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl
array
pop
communitycreator

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

okwudili onyejiaku

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"
Popping an element from an array in Perl

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
communitycreator
RELATED COURSES

View all Courses

Keep Exploring