Thursday, February 10, 2011

PERL : array operations

use Data::Dumper;
my @array1 = ("first","second","third","forth");
print "\nOriginal array : \n".Dumper(@array1);
push(@array1,"fifth");
print "\narray after push : \n".Dumper(@array1);
my $arrayPop = pop @array1;
print "\nElement extracted with POP : ".$arrayPop;
print "\nArray after POP command \n".Dumper(@array1);
unshift(@array1,"UNSHIFT");
print "\nArray after UNSHIFT : \n".Dumper(@array1);
my $arrayShift = shift @array1;
print "\nShift element from array : ".$arrayShift;
print "\nArray after SHIFT\n".Dumper(@array1);

Results:
Original array :
$VAR1 = 'first';
$VAR2 = 'second';
$VAR3 = 'third';
$VAR4 = 'forth';


array after push :
$VAR1 = 'first';
$VAR2 = 'second';
$VAR3 = 'third';
$VAR4 = 'forth';
$VAR5 = 'fifth';


Element extracted with POP : fifth
Array after POP command
$VAR1 = 'first';
$VAR2 = 'second';
$VAR3 = 'third';
$VAR4 = 'forth';


Array after UNSHIFT :
$VAR1 = 'UNSHIFT';
$VAR2 = 'first';
$VAR3 = 'second';
$VAR4 = 'third';
$VAR5 = 'forth';


Shift element from array : UNSHIFT
Array after SHIFT
$VAR1 = 'first';
$VAR2 = 'second';
$VAR3 = 'third';
$VAR4 = 'forth';

No comments:

Post a Comment