Perl Push

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


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





Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.