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:
@array1 = ( "Geeks" , "For" , "Geeks" );
print "Original Array: @array1\n" ;
$shifted_element = shift ( @array1 );
print "Shifted element: $shifted_element\n" ;
print "Updated Array: @array1" ;
|
Output:
Original Array: Geeks For Geeks
Shifted element: Geeks
Updated Array: For Geeks
Example 2:
@array1 = ( "Geeks" , "For" , "Geeks" );
print "Original Array: @array1\n" ;
$shifted_element = shift ( @array1 );
@array1 [3] = $shifted_element ;
print "Updated Array: @array1" ;
|
Output:
Original Array: Geeks For Geeks
Updated Array: For Geeks Geeks