- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
Pops off and returns the last value of the 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.
#!/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";
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
- Accessing Array Elements in Perl
- Slicing Array Elements in Perl
- Replacing Array Elements in Perl
- Removing empty array elements in PHP
- Removing duplicate elements from an array in PHP
- Add and Remove Elements in Perl Hashes
- Accessing Hash Elements in Perl
- Perl Array Variables
- Understanding Perl Array
- Array Size in Perl
- Removing Array Element and Re-Indexing in PHP
- Selecting Elements from Lists in Perl
- Adding two Python lists elements