Modifying List Elements

You can always add and remove list, array, and hash elements using the methods I described on Days 4and 5. But sometimes the standard ways of adding and removing elements from lists can be awkward, hard to read, or inefficient. To help with modifying lists, Perl provides a number of built-in functions, including

  • push and pop: add and remove list elements from the end of a list.

  • shift and unshift: add and remove list elements from the start of a list.

  • splice: add or remove elements anywhere in a list.

push and pop

The push and pop functions allow you to add or remove elements from the end of a list, that is, it affects the highest index positions of that list. Both push and pop will be familiar functions if you've ever worked ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.