Perl Variables  «Prev 

Perl Unshift Function

Question: How does the Perl unshift function work?

The unshift function in Perl is used to add elements to the beginning of an array. It modifies the original array by inserting the specified elements before the first element, effectively "shifting" the indexes of the existing elements. The unshift function returns the new number of elements in the array after the operation.
Here is the syntax for the unshift function:
unshift ARRAY, LIST;

  1. ARRAY: The array you want to modify by adding elements to the beginning.
  2. LIST: The list of elements you want to add to the beginning of the array.

Here is an example of how to use the unshift function in Perl:
#!/usr/bin/perl

my @array = ('b', 'c', 'd');
print "Original array: @array\n";

unshift @array, 'a';
print "Array after unshift: @array\n";

Program Ouput:
Original array: b c d
Array after unshift: a b c d

In this example, the unshift function is used to add the element 'a' to the beginning of the @array. After the operation, the @array contains the elements 'a', 'b', 'c', and 'd' in that order. Keep in mind that unshift modifies the original array, so if you need to preserve the original array, you should make a copy before using unshift.

Perl Shift function

The shift function in Perl is used to remove and return the first element from an array, effectively "shifting" the indexes of the remaining elements. It modifies the original array by removing the first element, and it returns the value of the removed element. If the array is empty, shift returns undef.
Here's the syntax for the shift function:
shift ARRAY;

ARRAY: The array you want to modify by removing the first element. If you use shift without specifying an array as an argument, it operates on the @_ array by default, which is the array of input parameters passed to a subroutine.
Here is an example of how to use the shift function in Perl:

Example 1:
#!/usr/bin/perl

my @array = ('a', 'b', 'c', 'd');
print "Original array: @array\n";

my $first_element = shift @array;
print "First element removed: $first_element\n";
print "Array after shift: @array\n";

Output:
Original array: a b c d
First element removed: a
Array after shift: b c d

In this example, the shift function is used to remove the first element ('a') from the @array. After the operation, the @array contains the elements 'b', 'c', and 'd' in that order.
Keep in mind that shift modifies the original array, so if you need to preserve the original array, you should make a copy before using shift.


The shift and unshift functions work just like pop and push.
However, from the other end of the array.

Perl shift function

The shift function takes the element from the bottom of the stack (element 0):

Diagram of shift
Diagram of shift

Perl unshift Function

The unshift puts an element on the bottom at element 0.
Diagram of unshift
Diagram of unshift

These functions aren't normally thought of in terms of a stack (although they certainly do work the same); rather they are commonly used to pass parameters to subroutines, including the main function of a Perl program. Their use in that capacity will be covered in detail later in the course.

shift and unshift

Note that shift and unshift will affect the sequence of the array more significantly (because the elements are taken from the first rather than last index). Therefore, care should be taken when using this pair of functions. However, the shift function is also the most practical when it comes to individually selecting the elements from a list or array, particularly the @ARGV and @_ arrays. This is because it removes elements in sequence: the first call to shift takes element 0, the next takes what was element 1, and so forth. The unshift function also has the advantage that it inserts new elements into the array at the start, which can allow you to prepopulate arrays and lists before the information provided. This can be used to insert default options into the @ARGV array, for example.