Perl Shift

Perl shift function is similar to perl pop and opposite to Perl Push. It removes and returns the first element of an array, shortening the dimension of the array with 1. Perl pop removes the last element in the array and Perl shift removes the first element.

Perl Shift Syntax

pop (ARRAY)
pop

Perl Shift Examples

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

The output of the above code is:

$size = 1, @array = 2 3 4

Post your Answer

  • Lines and paragraphs break automatically.
  • 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 <% ... %>.
  • Links to specified hosts will have a rel="nofollow" added to them.

More information about formatting options