Tuesday 4 March 2014

Perl Function Push,POP,Shift, Unshift.

1) chop() and chomp()
--------------------------

chomp
This is an alternative to the chop() function. It removes characters at the end of strings corresponding to the $INPUT_LINE_SEPARATOR ($/). It returns the number of characters removed. It can be given a list of strings upon which to perform this operation. When given no arguments, the operation is performed on $_.

chop
This function removes the last character of a string and returns that character. If given a list of arguments, the operation is performed on each one and the last character chopped is returned.

#! usr/bin/perl
print "How old are you?";
$age = <>;
print "What is your favorite color?";
$color = <>;
print "You are $age, and your favorite color is $color.";


#! usr/bin/perl
print "How old are you?";
chomp ($age = <>);
print "What is your favorite color?";
chomp ($color = <>);
print "You are $age, and your favorite color is $color.";


2) POP

Perl pop function is used to remove and return the last element of an array and is analogous with the strings chopfunction that removes the last character of a string. The size of the array will be shortened by 1. For arrays, it is similar with shift function that removes the first element of an array and is opposite to push/unshift function that adds a list at the end/front of an array.
Please note that whereas push function generally appends a list at the end of an array, pop function removes only the last element of the array, and not a list of elements.

The following code snippet will show you a simple example about how you can use it: 

#!/usr/bin/perl

use strict;
use warnings;

# initialize an array
my @numbers = qw(one two three four);
my $number = pop(@numbers);
# or my $number = $numbers[$#numbers-1];
# $#numbers--;
print "Removed: $number, \@numbers: @numbers\n";
# it displays Removed: four, @numbers: one two three

Applying Perl 
pop function has a similar effect as the two code lines:
my $number = $numbers[$#numbers-1];
$#numbers--;




3) PUSH
Perl push function is used to append a list to the right side of an array. It returns the number of elements of the array after pushing the elements. This function is similar withunshift that inserts a list of elements in front of an array and is the opposite of the pop/shift function that removes the last/first element of an array.
push and pop functions can manipulate an array like a stack. On the other side, both push and shift functions can be used to treat an array as a queue.
This short free tutorial will give you some examples about how you can use Perl push function in your script applications. But to begin with, let’s see its syntax form:
push (ARRAY, LIST)

The first argument of Perl 
push function will tell you which array to operate on, and the second lets you specify the elements to be added to the array. The parentheses are optional. It returns the number of the elements after appending the elements from the list. 

Now here are a few lines of code to see some examples. 

Appending a list to an array or one array to another


The next example will join an array with a list, using the 
push function:
#!/usr/bin/perl

use strict;
use warnings;

my @array = (1, 2, 3, 4);
my $size = push (@array, (5, 6, 7));
# or my $size = push (@array, 5, 6, 7);
# or my $size = push @array, (5, 6, 7);
# or my $size = push @array, 5, 6, 7;

print "\$size = $size, \@array = @array\n";

The output is as follows:
$size = 7, @array = 1 2 3 4 5 6 7

4)SHIFT
Perl shift function removes and returns the first element of an array, shortening the dimension of the array with 1.
This function is similar with pop function, which takes off the rightmost element of the array and opposite tounshift/push function that inserts a list at the beginning/end of the array.

In connection with 
unshiftpop and push, this function can be used to simulate queues and stacks in Perl.
In this short free tutorial I’ll show you how you can use Perlshift function inside your Perl script.
I’ll give you some code snippet examples to highlight some of the features of this function.


To begin with, Perl shift function has two syntax forms:
shift(ARRAY)
shift()

The parenthesis can be omitted, but if you want your code more readable or avoid some precedence issues, you can use them. Anyway, I suggest you to use strict and warnings in your script, to avoid any possible typos or other issues. If the array used as argument is empty, this function will return the
undef value.
The second form has no argument and in this case the Perl shift function will be applied on some special arrays:
·        @_ if you use it inside the lexical scope of a subroutine
·        @ARGV if you use it outside the body of a subroutine
Next, I’ll show you a simple example:
#!/usr/bin/perl

use strict;
use warnings;

# initialize an array
my @colors = qw(blue gray yellow white);
my $color = shift(@colors);
print "Removed: $color, Remained: @colors\n";
# it displays Removed: blue, Remained: gray yellow white

The first element of the array will be removed. 

5) UNSHIFT
The Perl unshift function inserts a list at the beginning of an array and returns the number of elements of the array after the insertion.
Practically, the whole list is prepended at the beginning of the array, so the prepended elements stay in the same order like in the original list.
If you want to reverse the order of elements in the list, you can use the reverse function.
The Perl unshift function is similar with push function, which appends the list at the end of the array and opposite to shift/pop function that takes off an element at the beginning/end of the array.
In connection with shiftpop and push, this function can be used to simulate queues and stacks in Perl.

If you have any concerns about how long it takes to run repeatedly the Perlunshift function with a larger list, you must know that an array is allocated larger than needed so that there is enough room to grow in both directions, either you use unshift or push to add new elements. Thus in most cases, there is no need for Perl to reallocate the entire structure array and so decrease the efficiency. For a deep digging about this subject, I suggest you to follow this




Now, the syntax form:
unshift (ARRAY, LIST)

The first argument of the Perl 
unshift function will tell you which array to operate on, and the second let you specify the elements to be added to the array. Like in the case of other built-in functions, the parentheses are optional.
The next short code snippet will show how you can use it:


#!/usr/bin/perl

use strict;
use warnings;

my @names = qw(John Peter Mary);
my $size = unshift @names, 'Alice', 'Tom';
# or my $size = unshift @names, ('Alice', 'Tom');
# or my $size = unshift (@names, 'Alice', 'Tom');

print "Size = $size, \@names = @names\n";
# displays Size = 5, @names = Alice Tom John Peter Mary

The next two examples are about how you can model an array like a stack or a queue by using the 
unshift function. 

No comments:

Post a Comment