Perl (Scripting) - Remove last element from an array (pop)

by
Jeremy Canfield |
Updated: June 22 2021
| Perl (Scripting) articles
Let's take for example the following array.
@fruit = qw(banana apple orange grapes);
Printing the array will print all of the elements in the array.
banana apple orange grapes
Pop can be used to remove the last element from the array. In this example, "grapes" is removed from the @fruit array.
pop @fruit;
Printing the array will show that "grapes" has been removed from the array.
banana apple orange
If needed, you can store the elements being removed from the array in a variable.
$grapes = pop @fruit;
Shift can be used to remove values from the beginning of an array.
Unshift can be used to append values to the beginning of an array.
Push can be used to append vaues to the end of an array.
Did you find this article helpful?
If so, consider buying me a coffee over at