Perl | Arrays (push, pop, shift, unshift)

Perl provides various inbuilt functions to add and remove the elements in an array.

Function Description
push Inserts values of the list at the end of an array
pop Removes the last value of an array
shift Shifts all the values of an array on its left
unshift Adds the list element to the front of an array
push function

 
This function inserts the values given in the list at an end of an array. Multiple values can be inserted separated by comma. This function increases the size of an array. It returns number of elements in new array.

Syntax: push(Array, list)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initalizing the array
@x = ('Java', 'C', 'C++');
  
# Print the Inital array
print "Original array: @x \n";
  
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
  
# Printing the array
print "Updated array: @x";

chevron_right


Output:

Original array: Java C C++ 
Updated array: Java C C++ Python Perl

 

pop function

 
This function is used to remove the last element of the array. After executing the pop function, size of the array is decremented by one element. This function returns undef if list is empty otherwise returns the last element of the array.

Syntax: pop(Array)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initalizing the array
@x = ('Java', 'C', 'C++');
  
# Print the Inital array
print "Original array: @x \n";
  
# Prints the value returned by pop
print "Value returned by pop: ", pop(@x);
  
# Prints the array after pop operation
print "\nUpdated array: @x";

chevron_right


Output:

Original array: Java C C++ 
Value returned by pop: C++
Updated array: Java C

 

shift function

 
This function returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop. This function returns undef if the array is empty otherwise returns first element of the array.

Syntax: shift(Array)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initalizing the array
@x = ('Java', 'C', 'C++');
  
# Print the Inital array
print "Original array: @x \n";
  
# Prints the value returned
# by shift function
print "Value returned by shift: ",
                        shift(@x);
  
# Array after shift operation
print "\nUpdated array: @x";

chevron_right


Output:

Original array: Java C C++ 
Value returned by shift :Java
Updated array: C C++

 

unshift function

 
This function places the given list of elements at the beginning of an array. Thereby shifting all the values in an array by right. Multiple values can be unshift using this operation. This function returns the number of new elements in an array.

Syntax: unshift(Array, List)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initalizing the array
@x = ('Java', 'C', 'C++');
  
# Print the Inital array
print "Original array: @x \n";
  
# Prints the number of elements
# returned by unshift
print "No of elements returned by unshift: ",
                   unshift(@x, 'PHP', 'JSP');
  
# Array after unshift operation
print "\nUpdated array: @x";

chevron_right


Output:

Original array: Java C C++ 
No of elements returned by unshift :5
Updated array: PHP JSP Java C C++


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.