1. Perl
  2. builtin functions
  3. shift

shift function - get the beginning of the array

Use the shift function to get the first element of the array . Note that the first element is truncated and is no longer in the original array.

$ret = shift @array;

Example

This is an example shift function. If @nums is (1, 2, 3), 1 is assigned to $first and @nums is (2, 3).

my @nums = (1, 2, 3);
my $first = shift @nums;
  1. Perl
  2. builtin functions
  3. here

unshift function - add element to the beginning of the array

You can use the unshift function to add an element to the beginning of array.

unshift @array, $value;

This is an example of the unshift function. If @nums was (1, 2, 3) then @nums would be (0, 1, 2, 3).

my @nums = (1, 2, 3);
unshift @nums, 0;

Related Informatrion