Perl | shift() Function

shift() function in Perl 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 the first element of the array.

Syntax: shift(Array)

Returns: -1 if array is Empty otherwise first element of the array

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Defining Array to be shifted
@array1 = ("Geeks", "For", "Geeks");
  
# Original Array
print "Original Array: @array1\n";
  
# Performing the shift operation
$shifted_element = shift(@array1);
  
# Printing the shifted element
print "Shifted element: $shifted_element\n";
  
# Updated Array
print "Updated Array: @array1";

chevron_right


Output:

Original Array: Geeks For Geeks
Shifted element: Geeks
Updated Array: For Geeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Program to move first element 
# of an array to the end
  
# Defining Array to be shifted
@array1 = ("Geeks", "For", "Geeks");
  
# Original Array
print "Original Array: @array1\n";
  
# Performing the shift operation
$shifted_element = shift(@array1);
  
# Placing First element in the end
@array1[3] = $shifted_element;
  
# Updated Array
print "Updated Array: @array1";

chevron_right


Output:

Original Array: Geeks For Geeks
Updated Array: For Geeks  Geeks


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.