Burning glass Technologies

Burning glass Technologies
Checktronix India Private Limited

Thursday, April 22, 2010

Perl Array

Today we can learn some thing about Array in perl.

An array variable starts with an @ symbol.

DEFINING AN ARRAY:

@num=(1,2,3,4);

@str=(‘hi’,’hello’,hey’);

COMBINING OF ARRAY:

Two array can be combined easily in perl

For Eg:

1.pl:

@num=(1,2,3,4);

@str=(‘hi’,’hello’,hey’);

@combine=(@num,@str);

Print “@combine\n”;

o/p: 1 2 3 4 hi hello hey

Simplify:

While declaring string in array we no need to waste time by giving quotes for that special quoting function is there qw.

Eg:

2.pl:

@color=qw(black green white);

CREATING A LIST USING RANGE OPERATOR:

@num=(1..20);

Will have no from 1 to 100

@letter=(‘a’..’z’);

One Example for swaping of numbers.

Its very easy in perl

3.pl

($a,$b)=(1,2);

print "before swap\n";

print "a=$a\n";

print "b=$b\n";

print "after swap";

($a,$b)=($b,$a);

print "after swap\n";

print "a=$a\n";

print "b=$b\n";

o/p:

before swap

a=1

b=2

after swap

a=2

b=1

TO FIND END OF ARRAY:

Just declare $#array name

4.pl

@num=(1..20);

print $#num;

o/p:

19.

Index starts from 0.

1 comment: