Perl

Perl “Push” Function

Multiple functions exist in Perl to insert the data in the Perl array. The push() function is one of the ways to insert the data in the Perl array. It is used to insert one or more values at the end of the array and return the total number of array values after adding the new data. The pop() function is related to this function that is used to remove the data from the end of the Perl array. The uses of the push function in Perl are shown in this tutorial.

Syntax:

The syntax of the push() function is given in the following. The function takes two arguments. The first argument is an array where the new value(s) is inserted and the second argument can be a scalar variable or a list of values or another array that is inserted in the array. This function returns the total number of elements of the array after insertion.

push(Array, scalar_variable)

Or

push(Array, List)

Push Function Examples

The method of inserting single and multiple values into a Perl array using the push() function is shown in this part of the tutorial.

Example 1: Add a Single String Value into the Array

In this example, an array of four string values is declared at the beginning of the script. The values of this array are printed by joining with the new line. Next, the push() function is called two times to insert two string values in the array. The values of the array are printed with the newline after inserting the new values.

#!/usr/bin/perl

use strict;

use warnings;

#Declare an array of string

my @books = ('Learning Perl', 'Programming Perl', 'Modern Perl', 'Advanced Perl Programming');

#Join the array values with a new line

my $output = join "\n", @books;

print "The original content of the array values: \n$output\n";

#Add two new elements with an array

push (@books, 'Perl Core Language: Little Black Book');

push (@books, 'Perl Best Practices');

#Join the array values with a newline after adding 2 elements by the push() function

$output = join "\n", @books;

print "\nThe content of the array values after insertion:\n$output\n";

Output:

The following output shows that two new values are inserted into the array:

p1

Example 2: Add Multiple Values into the Array

In this example, an array of four string values is declared at the beginning of the script. The values of this array are printed by joining with the comma(,). Next, a push() function is called to insert a list of three string values in the array. The values of the array are printed with the comma(,) after inserting the new values.

#!/usr/bin/perl

use strict;

use warnings;

#Declare an array of string

my @products = ('Fan', 'Light', 'Laptop', 'Table');

#Join the array values with a comma

my $output = join " , ", @products;

print "The original content of the array values: \n$output\n";

#Insert 3 new elements in the array

push(@products, ('Chair', 'Printer', 'Projector'));

#Join the array values with a comma after adding 3 elements by the push() function

$output = join " , ", @products;

print "\nThe content of the array values after insertion:\n$output\n";

Output:

The following output shows that three new values are inserted into the array:

p2

Example 3: Add a List of Values into a Reference Array

In this example, a reference array of an array of numbers is defined before adding values to the array. Next, the list of three numbers is inserted into the reference array using the push() function. Both the main array and the reference contain the same array values after the insertion. So, if any value is changed in the main array, the corresponding value of the reference array is changed also. Here, the value of the third index is changed in the main array. Both arrays are printed later.

#!/usr/bin/perl

use strict;

use warnings;

#Declare an array of numbers

my @numbers = (400, 567, 342, 907);

#Define the reference array of the @numbers array

my $ref_arr = \@numbers;

#Add three elements to the reference array

push (@{$ref_arr}, (876, 308, 123));

#Print the content of the reference array

print "The content of the reference array after adding elements:\n@{$ref_arr}\n";

#Modify the content of the second element of the main array

@numbers[2] = 100;

#Print the content of the main array

print "The content of the main array after modifying the array:\n@numbers\n";

#Print the content of the reference array

print "The content of the reference array after modifying the main array:\n@{$ref_arr}\n";

Output:

According to the following output, three new numbers are inserted in the main array and the reference array. When the third index of the main array is updated by the value of 100, the value of the third index of the reference array is changed to 100 also:


p3

Conclusion

The uses of the push() function are explained in this tutorial with multiple examples to help the Perl users to know the purpose of this function.

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.