Perl Push



Perl Push appends a list to the right side of an array. It returns the number of elements of the array after pushing. Perl push is opposite to perl pop or perl shift function. Perl pop removes the last element of an array.

Perl Push Syntax

push (array, list)

Perl Push Examples

!/usr/bin/perl
 
use strict;
use warnings;
 
my @array = (1, 2, 3, 4);
my $size = push (@array, (5, 6, 7));
 
print "\$size = $size, \@array = @array\n";

This would output following:

$size = 7, @array = 1 2 3 4 5 6 7

As in above example, we can concatenate another array as well(remember, list is an array).

#!/usr/bin/perl
 
use strict;
use warnings;
 
my @array = (1, 2, 3, 4);
my @newArray = (5, 6, 7);
push(@array, @newArray);
print "\@array = @array\n";

This would output following:

@array = 1 2 3 4 5 6 7