Abhijit K Rao Abhijit K Rao - 26 days ago 9
Perl Question

What is the difference between push and unshift in Perl?

Can someone please explain why push behaves the way as shown below?

Basically I am trying to print values of an array populated by

as well
unshift
.

When I try to print array contents populated by
push
using array indexes, It always prints the element at the top of the array, whereas array populated by
unshift
prints contents of array based on array index. I don't understand why.

with unshift



#!/usr/bin/perl
@names = ("Abhijit","Royal Enfield","Google");
@numbers=();
$number=1;
$i=0;
foreach $name (@names) {
#print $_ . "\n";
$number=$number+1;
#push(@numbers,($number));
unshift(@numbers,($number));
print("Array size is :" . @numbers . "\n");
$i=$i+1;
print("Individual Elements are:" . @numbers[i] . "\n");
pop(@numbers);
}

rhv:/var/cl_ip_down>./run.sh
Array size is :1
Individual Elements are:2
Array size is :2
Individual Elements are:3
Array size is :3
Individual Elements are:4


without unshift



#!/usr/bin/perl
@names = ("Abhijit","Royal Enfield","Google");
@numbers=();
$number=1;
$i=0;
foreach $name (@names) {
#print $_ . "\n";
$number=$number+1;
push(@numbers,($number));
#unshift(@numbers,($number));
print("Array size is :" . @numbers . "\n");
$i=$i+1;
print("Individual Elements are:" . @numbers[i] . "\n");
}

rhv:/var/cl_ip_down>./run.sh
Array size is :1
Individual Elements are:2
Array size is :2
Individual Elements are:2
Array size is :3
Individual Elements are:2





/without pop/

#!/usr/bin/perl
@names = ("Abhijit","Royal Enfield","Google");
@numbers=();
$number=1;
$i=0;
foreach $name (@names) {
#print $_ . "\n";
$number=$number+1;
#push(@numbers,($number));
unshift(@numbers,($number));
print("Array size is :" . @numbers . "\n");
$i=$i+1;
print("Individual Elements are:" . @numbers[i] . "\n");
#pop(@numbers);
}

rhv:/var/cl_ip_down>./run.sh
Array size is :1
Individual Elements are:2
Array size is :2
Individual Elements are:3
Array size is :3
Individual Elements are:4


with pop



#!/usr/bin/perl
@names = ("Abhijit","Royal Enfield","Google");
@numbers=();
$number=1;
$i=0;
foreach $name (@names) {
#print $_ . "\n";
$number=$number+1;
#push(@numbers,($number));
unshift(@numbers,($number));
print("Array size is :" . @numbers . "\n");
$i=$i+1;
print("Individual Elements are:" . @numbers[i] . "\n");
pop(@numbers);
}

rhv:/var/cl_ip_down>./run.sh
Array size is :1
Individual Elements are:2
Array size is :1
Individual Elements are:3
Array size is :1
Individual Elements are:4

Answer Source

You really should be using use strict; and use warnings; in your code. Having them activated will allow you to identify errors in your code.

Change all instances of the following:

foreach $name (@names) -> for my $i (@names) as you don't do anything with the elements in the @names array.

@numbers[i] -> $numbers[$i] as this is where you've made a not uncommon mistake of using an array slice rather than referring to an array element.

This is not C. Every 'variable' has to have a sigil ($, @, %, &, etc.) in front of it. That i should really be $i.


As for the difference between push and shift, the documentation explains:

perldoc -f push:

push ARRAY,LIST

Treats ARRAY as a stack, and pushes the values of LIST onto the end of ARRAY. The length of ARRAY increases by the length of LIST. ... Returns the number of elements in the array following the completed "push".

perldoc -f unshift:

unshift ARRAY,LIST

Does the opposite of a shift. Or the opposite of a push, depending on how you look at it. Prepends list to the front of the array, and returns the new number of elements in the array.


To put it ASCII-matically...

        +---------+           +-----------+        +---------+ 
<-----  | ITEM(S) |  ----->   | (@) ARRAY | <----- | ITEM(S) | ----->
 shift  +---------+  unshift  +-----------+  push  +---------+   pop
                              ^           ^
                              FRONT       END