Perl shift Function



Description

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.

Syntax

Following is the simple syntax for this function −

shift ( [ARRAY] )

shift

Return Value

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

Example

Following is the example code showing its basic usage −

#!/usr/bin/perl

@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
perl_function_references.htm
Advertisements