Perl | unshift() Function

unshift() function in Perl places the given list of elements at the beginning of an array. Thereby shifting all the values in the 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)

Returns: Number of new elements in Array

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl 
    
# Initalizing the array 
@x = ('Geeks', 'for', 'Geeks'); 
    
# 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, 'Welcome', 'to'); 
    
# Array after unshift operation 
print "\nUpdated array: @x"

chevron_right


Output:

Original array: Geeks for Geeks 
No of elements returned by unshift: 5
Updated array: Welcome to Geeks for Geeks


Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl 
    
# Initalizing the array 
@x = (10, 20, 30, 40, 50); 
    
# 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, 70, 80, 'Geeks'); 
    
# Array after unshift operation 
print "\nUpdated array: @x"

chevron_right


Output:

Original array: 10 20 30 40 50 
No of elements returned by unshift: 8
Updated array: 70 80 Geeks 10 20 30 40 50


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.