Prepend one or more elements to the beginning of an array.
The unshift function adds one or more values to the beginning of an array and returns the new number of elements in the array. All existing elements are shifted to higher indices to make room.
unshift is the counterpart of shift. While shift removes from the front, unshift adds to the front. This pair allows arrays to be used as FIFO (First In, First Out) queues when combined with pop.
Multiple values can be prepended in a single call, and they are added in the order given, so the first value in the argument list becomes the first element of the array.
| Name | Description | Optional |
|---|---|---|
ARRAY |
The array to prepend to. | No |
LIST |
One or more values to prepend to the array. | No |
my @nums = (3, 4, 5);
unshift @nums, 1, 2;
# @nums is now (1, 2, 3, 4, 5)