How to pop an element or a value from an array in Perl
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 popprint "After pop: \@gnaam = @languages\n";# print popped valueprint "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.