Perl: Array operations, push,pop,shift,unshift,scalar keyword.

#Array operations PUSH,#POP,shift,unshift,sorting #and use of scalar #keyword for
#getting the length of array;
#arr1 is 2 dimensional array.
use strict;

my @arr1 = ( [“kk”, “KJK”, “KD”],
[“MK”, “MJK”, “MN”]) ;

print “Original arr1 is :\n” ;
print @$_,”\n \n” foreach @arr1;

print “\n\n added element using unshift operator”;
unshift (@arr1[0] , “kedarKOOL”);
print “\n\n now arr1 is:\n”;
print @$_, foreach @arr1 ;

print “\n\n removed element using shift operator”;
shift (@arr1[0]);
print “\n\n now arr1 is\n”;
print @$_,”\n” foreach @arr1 ;

print “now push pop results” ;

my @arr2 = ( “MANDAR4″,”KK”,”3d”,”KEDAR1″);
print “\n \n original arr2 is :\n” ;

foreach my$arr2(@arr2) {
print $arr2,”\t”;
}

push (@arr2,”kjk”);
print “\n\n added element using push operator. \n Now arr2 is:\n” ;

foreach my $arr2element (@arr2)
{ print “\t”, $arr2element ;
} ;

print “\n\n removed element using pop operator. \n Now arr2 is:\n” ;
pop (@arr2);
print “\n” ;
foreach my $arr2element (@arr2)
{ print “\t”, $arr2element ;
} ;

print “\n\n Joining the array with :: looks like this ->”, join (“::”,@arr2);
my $str1 = ” This is one string”;
print “\n”,$str1;
my @arr4 = split / /,$str1;
print “\n Strings splitted array is:”;
foreach my $arr4 (@arr4)
{
print $arr4, “\t”;
}

my @arr3 = @arr2[0,2,3];
print “\n\n original arr3 is:\n @arr3”;

@arr2 = sort(@arr3);
print “\n sorted arr3 is :\n @arr2”;

print “\n Length of arr2 is :”,scalar(@arr2), “\n”;
print ” Length of arr1 is :”,scalar(@arr1), “\n”;

Advertisements

Tags: , , , , , , ,

3 Responses to “Perl: Array operations, push,pop,shift,unshift,scalar keyword.”

  1. koolkedar Says:

    Output of the above script is:
    Original arr1 is :
    kkKJKKD

    MKMJKMN

    added element using unshift operator

    now arr1 is:
    kedarKOOLkkKJKKDMKMJKMN

    removed element using shift operator

    now arr1 is
    kkKJKKD
    MKMJKMN
    now push pop results

    original arr2 is :
    MANDAR4 KK 3d KEDAR1

    added element using push operator.
    Now arr2 is:
    MANDAR4 KK 3d KEDAR1 kjk

    removed element using pop operator.
    Now arr2 is:

    MANDAR4 KK 3d KEDAR1

    Joining the array with :: looks like this ->MANDAR4::KK::3d::KEDAR1
    This is one string
    Strings splitted array is: This is one string

    original arr3 is:
    MANDAR4 3d KEDAR1
    sorted arr3 is :
    3d KEDAR1 MANDAR4
    Length of arr2 is :3
    Length of arr1 is :2

  2. Michele Says:

    Usually I don’t read post on blogs, however I wish to say that this write-up very forced me to take a look at and do it! Your writing taste has been surprised me. Thanks, very great post.

  3. koolkedar Says:

    Unshift operator adds an element at the begining of an array.
    Shift operator removes an element from the begining of an array.

    Push operator adds an element at the end of the array.
    Pop operator removes an element at the end of the array.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: