shift and unshift are working on the beginning (left side) of the array.
shift fetches the first element of an array.
It returns the fetched element and the whole array becomes one shorter and moved
to the left. Returns undef if the array was empty.
unshift adds element(s) to the beginning of an array
returns number of elements in the array after the addition
Example:
Example 8-6. examples/arrays/shift_unshift.pl
#!/usr/bin/perl
use strict;
use warnings;
my @names = ("Foo", "Bar", "Baz");
my $first = shift @names;
print "$first\n"; # Foo
print "@names\n"; # Bar Baz
unshift @names, "Moo";
print "@names\n"; # Moo Bar Baz
FIRST = shift ARRAY;
unshift ARRAY, VALUEs;
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| stack (pop, push) | Up | queue (shift, push) |