Updated April 4, 2023
Introduction to Perl push
In Perl, the push() function is defined as a function to stack up on the elements or add elements to the existing set of items used in implementing the stack or array of elements for either adding or removing the elements using push and pop functions in Perl. In general, the push() function can be defined as an array or stack function for inserting or adding the set of items or values to another set or array of items where this push function is independent of the type of values passed in the set of elements which means the set of elements can have alphabets, numerical or both alpha-numeric values.
Working of push() function in Perl with Examples
In this article, we will discuss the push() function provided by Perl for inserting or adding a set of elements to the existing stack or array of elements which is mainly used as a stack function along with the pop() function, which does the opposite of push() function which means the pop() removes or deletes the set or list of elements from the stack or array of elements. In Perl language, the push() function is defined as the insertion of items to the array or stack that is passed as an argument to this push() function, and also we need to pass another array to store these added items to the defined array and returns the array with added elements.
Now let us see the syntax and Parameters of the push() function in Perl:
Syntax:
push( arr_name, res_list)
Parameters:
arr_name: This parameter is used to specify the array name that is declared at the beginning of the code, which already has elements, and the elements are added to this array.
res_list: This parameter is used to specify the set or list of elements separated by a comma that needs to be added to the array defined, which is the first argument to the push() function.
This push() function returns again a set of elements as a new array having the elements of the previous array along with the added elements to this previous or defined array.
Examples of push() function in Perl
Now let us see a simple example of how to demonstrate the push() function in Perl:
Example 1
Code:
#!/usr/bin/perl
print "Demonstration of push() function in Perl:";
print "\n";
print "\n";
@arr1 = ('Educba ', 'Google ', 'Opera ');
print "The first array is given as follows: ";
print "\n";
foreach $x (@arr1)
{
print "$x";
print "\n";
}
print "\n";
@arr2 = (20, 40, 60);
print "The second array is given as follows: ";
print "\n";
foreach $r (@arr2)
{
print "$r";
print "\n";
}
print "\n";
print "After applying push() function to the given arrays";
print "\n";
push(@arr1, 'Explorer ', 'UC ');
push(@arr2, (80, 100 ));
print "The new array after arr1 is applied with push() is as follows:";
print "\n";
foreach $y (@arr1)
{
print "$y";
print "\n";
}
print "\n";
print "The new array after arr2 is applied with push() is as follows:";
print "\n";
foreach $s (@arr2)
{
print "$s";
print "\n";
}
Output:
In the above program, we are declaring two different arrays one contains string type, and another contains numeric type values. In the above code, we first print all the elements in the array that is declared at the beginning, and then later, we call the push function. We pass the array such as arr1 and arr2, followed by the elements that need to be added to the previously declared array. Then we are printing the new array that has extra elements ( “Explorer” and “UC to arr1, “80” and “100” to arr2) along with the previous elements in both the arrays arr1 and arr2 now contains the elements that were pushed to the previously declared arrays using push() function such as arr1 and arr2 will now have 5 elements in them as arr1 = (‘Educba’, ‘Google’, ‘Opera’, ‘Explorer’, ‘UC’) and arr2 = (20, 40, 60, 80, 100). The output shows the new array, and the values are printed using the foreach loop.
In the above code, we should note that we have passed arguments to the push() function, but if we do not pass any arguments to the function, it will throw an error saying pass enough arguments to execute the push() function Perl. We can see in the below screenshot of the output of the above code that is modified where “push()” only called instead of any other arguments.
Now let us another use of the push() function in Perl language such as adding elements to the given array as done in the above example, joining two arrays irrespective of data types of the values in the array or appending of two arrays, and we can add elements to an array reference using push() function let us see in the below example and note that we can even push multiple values or elements directly to the array.
Example 2
Code:
#!/usr/bin/perl
print "Demonstration of use of push() function in Perl:";
print "\n";
print "\n";
@arr1 = ('Educba ', 'Google ', 'Opera ');
print "The first array is given as follows: ";
print "\n";
foreach $x (@arr1)
{
print "$x";
print "\n";
}
print "\n";
@arr2 = (20, 40, 60);
print "The second array is given as follows: ";
print "\n";
foreach $r (@arr2)
{
print "$r";
print "\n";
}
print "\n";
print "After applying push() function to the given arrays";
print "\n";
push(@arr1, 'Explorer ', 'UC ');
print "\n";
print "After applying push() function directly";
print "\n";
push @arr2, (50, 70);
print "\n";
print @arr2;
print "\n";
print "Applying push() function for joining or appending two arrays";
print "\n";
push(@arr1,@arr2);
print @arr1;
print "\n";
print "\n";
print "Applying push() function for adding to array reference";
print "\n";
my @arr = qw(Python Perl);
my $arr_ref = \@arr;
push ( @{ $arr_ref }, qw(Java Ruby));
print "@{ $arr_ref }\n";
exit 0;
Output:
In the above program, we can see we are using the push function directly to add the elements, and we also saw how to push() can be used for joining two arrays, and we also saw how we could add elements to the array reference. The output is as shown in the above screenshot.
Conclusion
In this article, we conclude that Perl’s push() function is used to push the elements to the array elements. In this article, we saw a simple example of how the push() function is used on different types of values. In this, we also saw various use of the push() function in appending of the arrays, and we also saw how to add elements to the array reference.
Recommended Articles
This is a guide to Perl push. Here we discuss the Working of the push() function in Perl with Examples and how the push() function is used on different types. You may also have a look at the following articles to learn more –