Perl shift Function



This function returns the first value in an array, deleting it and shifting the elements of the array list to the left by one. If ARRAY is not specified, shifts the @_ array within a subroutine, or @ARGV otherwise. shift is essentially identical to pop, except values are taken from the start of the array instead of the end.


Following is the simple syntax for this function −

shift ( [ARRAY] )


Return Value

This function returns undef if the array is empty else returns first element in array.


Following is the example code showing its basic usage −


@array = (1..5);
while ($element = shift(@array)) {
    print("$element - ");
print("The End\n");

When above code is executed, it produces the following result −

1 - 2 - 3 - 4 - 5 - The End