- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding and Removing Elements in Perl Array
Perl provides a number of useful functions to add and remove elements in an array. You may have a question what is a function? So far you have used the print function to print various values. Similarly, there are various other functions or sometimes called subroutines, which can be used for various other functionalities.
Sr.No. | Types & Description |
---|---|
1 | push @ARRAY, LIST Pushes the values of the list onto the end of the array. |
2 | pop @ARRAY Pops off and returns the last value of the array. |
3 | shift @ARRAY Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. |
4 | unshift @ARRAY, LIST Prepends list to the front of the array, and returns the number of elements in the new array. |
Example
#!/usr/bin/perl # create a simple array @coins = ("Quarter","Dime","Nickel"); print "1. \@coins = @coins\n"; # add one element at the end of the array push(@coins, "Penny"); print "2. \@coins = @coins\n"; # add one element at the beginning of the array unshift(@coins, "Dollar"); print "3. \@coins = @coins\n"; # remove one element from the last of the array. pop(@coins); print "4. \@coins = @coins\n"; # remove one element from the beginning of the array. shift(@coins); print "5. \@coins = @coins\n";
Output
This will produce the following result −
1. @coins = Quarter Dime Nickel 2. @coins = Quarter Dime Nickel Penny 3. @coins = Dollar Quarter Dime Nickel Penny 4. @coins = Dollar Quarter Dime Nickel 5. @coins = Quarter Dime Nickel
- Related Articles
- Accessing Array Elements in Perl
- Slicing Array Elements in Perl
- Replacing Array Elements in Perl
- Removing empty array elements in PHP
- Adding or removing elements from a list dynamically using VueJS?
- Removing duplicate elements from an array in PHP
- Removing redundant elements from array altogether - JavaScript
- Add and Remove Elements in Perl Hashes
- Adding elements to array to make its sum diverse in JavaScript
- Program to find mean of array after removing some elements in Python
- Accessing Hash Elements in Perl
- Array Size in Perl
- Reduce array's dimension by adding all the elements in Numpy
- Removing Array Element and Re-Indexing in PHP
- Selecting Elements from Lists in Perl

Advertisements