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

25 May, 2021
Perl provides various inbuilt functions to add and remove the elements in an array.  
 
 
  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:  
Perl
#!/usr/bin/perl # Initializing the array@x = ('Java', 'C', 'C++'); # Print the Initial arrayprint "Original array: @x \n"; # Pushing multiple values in the arraypush(@x, 'Python', 'Perl'); # Printing the arrayprint "Updated array: @x";
Output:  
Original array: Java C C++ 
Updated array: Java C C++ Python Perl
   
  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:  
Perl
#!/usr/bin/perl # Initializing the array@x = ('Java', 'C', 'C++'); # Print the Initial arrayprint "Original array: @x \n"; # Prints the value returned by popprint "Value returned by pop: ", pop(@x); # Prints the array after pop operationprint "\nUpdated array: @x";
Output:  
Original array: Java C C++ 
Value returned by pop: C++
Updated array: Java C
   
  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:  
Perl
#!/usr/bin/perl # Initializing the array@x = ('Java', 'C', 'C++'); # Print the Initial arrayprint "Original array: @x \n"; # Prints the value returned# by shift functionprint "Value returned by shift: ",                        shift(@x); # Array after shift operationprint "\nUpdated array: @x";
Output:  
Original array: Java C C++ 
Value returned by shift :Java
Updated array: C C++
   
  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:  
Perl
#!/usr/bin/perl # Initializing the array@x = ('Java', 'C', 'C++'); # Print the Initial arrayprint "Original array: @x \n"; # Prints the number of elements# returned by unshiftprint "No of elements returned by unshift: ",                   unshift(@x, 'PHP', 'JSP'); # Array after unshift operationprint "\nUpdated array: @x";
Output:  
Original array: Java C C++ 
No of elements returned by unshift :5
Updated array: PHP JSP Java C C++
 
sweetyty
Perl-Arrays
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl | split() Function
Perl | push() Function
Perl | chomp() Function
Perl | substr() function
Perl | grep() Function
Perl | exists() Function
Perl Tutorial - Learn Perl With Examples
Perl | length() Function
Perl | Subroutines or Functions
Use of print() and say() in Perl