Updated April 4, 2023
Introduction to Perl shift
In Perl, shift() function is defined as a function for shifting the elements or items in an array from left to right one at a time which does it by fetching the first item of the array instead of the last item as in pop function and removes this first element from the array or any set of the elements. In general, this shift() function is defined as an operation for shifting the first element from left to right and returns this element. If the array is empty and shift operation is applied to this empty array, it returns an undef value.
Working of shift() function in Perl
This article will discuss Perl’s shift() function for deleting or removing the elements from left to the right or removing elements from starting in the set of items or elements in an array. The shift operation is used for shifting the elements from left to the right one at a time, which means this removes the elements from the start of the stack of elements instead of the end of the stack of elements as done in the pop function in Perl. This shift operation returns undef if the array passed to shift() function is empty. This function is used not only to remove or delete elements but also for shifting the first element with any other element, or we can say replacing the first element with the next element in the set of elements or array.
Now let us see syntax and examples of shift() function in the below section:
Syntax:
Shift(arr_name);
Parameter:
arr_name: this parameter is used to specify the array which contains a set of elements from which the element needs to be removed or deleted.
This function returns the first element of the array, which is passed as a parameter to the shift() function. Suppose there is no parameter or array is passed to the shift() function. In that case, it returns the default values according to the location of the shift, such as if the shift function is specified outside the function, then it returns the first element of @argv. If the shift is specified inside any function, it returns the first element of the parameter passed to the function.
Examples of Perl shift
Here are the following examples mention below
Example #1
Code:
#!/usr/bin/perl -w
print "Demonstration for shift() function in Perl";
print "\n";
print "\n";
my @str_arr = ("Educba ", "Institute ", "Delhi ");
print "The given array is as follows: ";
print "\n";
print @str_arr;
print "\n";
$shft_ele = shift(@str_arr);
print "Element in array after applying shift() functiont: ";
print "\n";
print $shft_ele;
print "\n";
print "The array after removing the element is: ";
print "\n";
print @str_arr;
Output:
In the above program, we can see we have declared an array “str_arr”, which contains 3 elements in this array; then, we are printing the above array to see the original array with 3 elements having the first element “, Educba”. Then we have applied the shift() function in which we are removing the first element, and that element is stored in the variable “shft_ele”, and we have passed this array as a parameter to the shift() function. Then we can see it will only print the first element. Then we are printing the altered array after removing the first element is printed after applying the shift() function to the given array, where this array has the same name as the given array itself. Still, only the first element is deleted, and the given array is updated with only 2 elements after applying the shift() function to the given array that had 3 elements. The output of this program can be seen in the above screenshot.
Now let us see another example where we are specifying the shift() function without passing the parameter to this function.
Example #2
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
print "Demonstration for shift() function without parameter in Perl";
print "\n";
print "\n";
print "The shift() function is specified inside the function";
print "\n";
sub func_arr{
my $str_ele = shift;
print $str_ele;
print "\n";
}
print "The element that is removed after applying the shift() function";
print "\n";
my @str_arr = ('Java', 'Python', 'Ruby', 'Fotan');
print "The given array is as follows: ";
print "\n";
print @str_arr;
print "\n";
print "The removed element is as follows :";
func_arr(@str_arr);
Output:
In the above program, we can see we have first defined a function in which we are declaring a variable “$str_ele” in which we are defining a shift() function we can observe we have not passed any arguments to this shift() function which is defined inside the function and after applying the shift() function it returns the first element of the array specified below. Then we have declared an array with array name as “@str_arr”. So when we pass the elements or the array name to the function defined as “func_arr()”, so when this function is called the shift() function declared within this function will return only the first element of the parameter list or array. So by default, the shift() function, when declared inside the function with no arguments, then it takes the first parameter as an element to be returned or removed. The output is as shown in the above screenshot.
Now we will see how the shift() function is even used for replacing the element usually; it can be done only with the first element of the array or set of items.
Example #3
Code:
#!/usr/bin/perl -w
print "Demonstration of shift() function for replacing element";
print "\n";
print "\n";
@arr = ("Educba ", "Institute ", "Delhi ", "India ");
print "The given Array is as follows: ";
print "\n";
print @arr;
print "\n";
$shft_ele = shift(@arr);
print "The element to be shifted is: ";
print $shft_ele;
print "\n";
@arr[3] = $shft_ele;
print "The element will be placed at the end ";
print "\n";
print "The altered array after replacement is: ";
print @arr;
Output:
In the above program, we can see we have declared an array where the first element in that array is “Educba” we are using the shift() function to shift the first element at the end of the given array. So in the above code, we can see in the output the altered array displays the array with the first element as the last element.
Conclusion
This article concludes that the shift() function in Perl is used for the shifting element. This article defines the shift() function, which returns the first element from the array, which is passed as the argument to the shift() function. In this article, we saw examples of using and demonstrating shift() function with parameter and without parameter, and we also saw an example for shifting the first element at the end of the element.
Recommended Articles
This is a guide to the Perl shift. Here we discuss the Working of shift() function in Perl along with the examples and outputs. You may also have a look at the following articles to learn more –