Perl

Perl “Shift” Function

Perl has many functions to read the data from the Perl array. The shift function is one of them. It is used to remove the first element of the array. So, it works on FIFO (First In First Out) method. This function can be used with the argument and without any argument. An array is used as the argument of this function. The uses of the Perl shift function with an array and without any argument are shown in this tutorial.

Syntax:

The syntax of the “shift” function is given as follows:

shift

Or

shift ARRAY

Different Uses of the “Shift” Function

Different ways of using the “shift” function to read the argument values and array values are shown in this part of the tutorial.

Example 1: Use of the “Shift” Function to Read the Command Line Arguments

The command line argument values can be read using the “shift” command without any argument. The @ARGV array is used to store the command line argument values. In the following script, the total number of the command line argument values is counted using the $#ARGV variable. Next, the “for” loop is used to read all the argument values using the “shift” function without any argument.

#!/usr/bin/perl

use strict;

use warnings;

#Declare a variable to store the argument value

my $var;

#Count the total number of arguments

my $total_arguments = $#ARGV + 1;

#Print the total number of arguments

print "Total arguments: $total_arguments\n";

#Read and print the argument values

for(my $i = 1; $i <= $total_arguments; $i++)

{

  #read the argument

  $var = shift;

  #print the argument

  print "Argument $i: $var\n";

}

Output:

According to the following output, 67, 34, and 23 are given as the argument values during the execution of the script which is printed as follows:

p1

Example 2: Use of the “Shift” Function to Read the Function Arguments

In this example, an array of five string values is declared and this array is passed as the argument of the subroutine named read_func(). The “shift” function without an argument is used inside the function to read the first element of the array. The array values are printed before and after calling the function.

#!/usr/bin/perl

use strict;

use warnings;

#Declare an array of string

my @os = ('Ubuntu', 'CentOS', 'Kubuntu', 'Fedora', 'RedHat');

print "The array values before calling the function: \n @os\n";

#Call the function by passing the array as an argument

read_func(@os);

print "\nThe array values after calling the function: \n @os\n";

#Declare the function that will shift one array value

sub read_func {

  my $element = shift;

  print "\nThe retrieved value is $element.\n";

}

Output:

The following output appears after executing the script. According to this output, the “shift” function just read the first element of the array but didn’t remove the element from the array:

p2

Example 3: Use of the “Shift” Function with an Array

In this example, the “shift” function is used with an array that removes the first element of the array. An array of five elements is declared in the script. The “shift” function is executed with the array as an argument. The array values are printed before and after calling the “shift” function.

#!/usr/bin/perl

#Declare an array of string

my @os = ('Ubuntu', 'CentOS', 'Kubuntu', 'Fedora', 'RedHat');

#Print array before the shift

print "The array values before calling the shift function: \n @os\n";

#Remove the first element from the left side of the array

my $element = shift(@os);

print "\n$element is removed from the array.\n";

#Print array after shift

print "\nThe array values after calling the shift function: \n @os\n";

Output:

The following output appears after executing the script. According to this output, the “shift” function removed the first element from the array:

p3

Example 4: Use of the “Shift” Function in the Loop

In this example, the “shift” function is used with a “while” loop to read the array value by removing the value from the array in each iteration of the loop. An array of five numbers is defined in the script. The array values are printed before and after executing the loop.

#!/usr/bin/perl

use strict;

use warnings;

#Declare a numeric array

my @num_arr = (90, 23, 78, 19, 56);

#Print the array values

print "Array values before shift: \n@num_arr\n";

print "The shifted array values:\n";

#shift each element of the array using the loop

while (my $value = shift(@num_arr)) {

  print("$value ");

}

print "\nArray values after shifting :\n";

#Check the total number of elements after the shift

if($#num_arr < 0)

{

  print "The array is empty.\n";

}

else

{

  print "@num_arr\n";

}

Output:

The following output appears after executing the script. According to this output, the “shift” function removed all elements from the array after executing the block of the “while” loop:


p4

Conclusion

The uses of the “shift” function to read the command line argument values, function argument, and array values are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.