Jun 11, 2013

Shift Unshift in Perl

Shift/Unshift => It works at the start of the array

Shift     => It removes element from start of an array
Unshift => It appends element to start of an array

use strict;
use warnings;

my @names = ("Foo", "Bar", "Baz");

print "\n Array : " . Dumper(\@names);

my $first = shift @names;     #Shift - removes element at the start of the array

print "$first";               #Foo
print "\n After Shift : " . Dumper(\@names);   #Bar Baz

unshift @names, "Moo";         # UnShift - adds element at the start of the array
print "\n After UnShift : " . Dumper(\@names);   # Moo Bar Baz  

No comments:

Post a Comment