What is the difference between push and unshift in Perl?

Can someone explain why push behaves as shown below?

I am basically trying to print the values ​​of an array filled with push as well as unshift .

When I try to print the contents of an array filled with push using indexes of arrays, it always prints the element at the top of the array, while the array filled with unshift prints the contents of the array based on the index of the array. I do not 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 
+6
push perl pop
source share
5 answers

You really have to use use strict; and use warnings; in your code. Once activated, they will allow you to identify errors in your code.

Change all instances of the following items:

foreach $name (@names) β†’ for my $i (@names) since you are not doing anything with the elements in the @names array.

@numbers[i] β†’ $numbers[$i] , because here you made an unusual mistake when using a slice of an array, rather than referring to an array element.

This is not C. Each "variable" must have a sigil in front of it ( $ , @ , % , & , etc.). This i should really be $i .


Regarding the difference between push and shift , the documentation explains:

perldoc -f push :

press ARRAY, LIST

Treats ARRAY as a stack and pushes LIST values ​​to the end of the array. The length of ARRAY increases along the length of LIST .... Returns the number of elements in the array after the completed push.

perldoc -f unshift :

unshift ARRAY, LIST

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


For this, ASCII-matically ...

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

unshift used to add a value or values ​​to the beginning of an array:

The opposite of shift . Or vice versa push , depending on how you look at it.

The new values ​​then become the first elements in the array.

push adds elements to the end array:

ARRAY as a stack and pops LIST values ​​at the end of ARRAY .

+13
source share

It should be a comment, but it is too long for the comment field, so here it is.

If you want to illustrate the difference between unshift and push , it would be enough:

 #!/usr/bin/perl use strict; use warnings; my @x; push @x, $_ for 1 .. 3; my @y; unshift @y, $_ for 1 .. 3; print "\@x = @x\n\@y = @y\n"; 

Output:

  @x = 1 2 3
 @y = 3 2 1 

Note use strict; protects you from many programmer errors and use warnings; warns you when you use constructions of dubious value. At your level, none of them is mandatory.

+7
source share

note that

the pre-allocated array is balanced towards the end of the end of the array (which means there is more free space at the far end of the list than before the list item 0). This is done on purpose to make shreds more effective than unshifts . http://www.perlmonks.org/?node_id=17890

Despite the fact that lists do everything wonderful, because "Perl is intelligently encoded because it was expected to use lists as queues (in the same place)."

For comparison, in different JavaScript machines, shift / unshift on arrays seems to be significantly slower .

0
source share

I did not see any formulation of what these methods really do from the point of view of operational complexity, which helped me with conceptualization: quintessence, I think that it is called a β€œshift”, because in fact it should shift all n elements in your array to the new indexes to correctly update the length property.

push () and pop () use more complex operational complexity. No matter how many n values ​​in your array or your array.length, push or pop will always perform 1 operation. It does not need to deal with indexes, it does not require iteration, it only needs to perform one operation, always at the end of the stack, either adding or removing the value and index.

Most importantly, note when using push / pop that the other elements in the array are not affected - they are the same values ​​in the same metrics of your array. The length of the array is also automatically updated correctly, what do you expect when deleting or adding values.

On the other hand, shift () and unshift () not only add or remove, but should also actually "shift" all the other elements in your array by different indices. This is more complicated and takes longer because the number of operations depends on n, the number of elements in your array or array.length. For each n + 1 more, he must do 1 more operation to transfer each of the values ​​to the correct index, having correctly edited the length.

Otherwise, if he did not perform n operations after shift () and moved other elements, you will not have an element with index 0, and it will not change the length of your array, will it? We want the length of our arrays to be updated intuitively, and shift and unshift must do more operations to do this.

0
source share

All Articles