Saturday, May 31, 2014

Illustration of push, pop, unshift and shift in an array

Script

#!/usr/bin/perl

@planets = ("Venus","Earth","Mars","Jupiter","Saturn","Uranus");

print "\n";
print "@planets \n\n";
print "Use PUSH to include one entry at the end \n";
push(@planets, "Neptune");
print "@planets \n\n";
print "Use POP to get rid of the last entry at the end \n";
pop(@planets);
print "@planets \n\n";
print "Use UNSHIFT to include one entry at the beginning of the array \n";
unshift(@planets, "Mercury");
print "@planets \n\n";
print "Use SHIFT to get rid of the first entry at the beginning of the array \n";
shift(@planets);
print "@planets \n";


Execution


Venus Earth Mars Jupiter Saturn Uranus

Use PUSH to include one entry at the end
Venus Earth Mars Jupiter Saturn Uranus Neptune

Use POP to get rid of the last entry at the end
Venus Earth Mars Jupiter Saturn Uranus

Use UNSHIFT to include one entry at the beginning of the array
Mercury Venus Earth Mars Jupiter Saturn Uranus

Use SHIFT to get rid of the first entry at the beginning of the array
Venus Earth Mars Jupiter Saturn Uranus

No comments:

Post a Comment