One Dimensional Array:-
Arrays are great in Perl, we can store heterogeneous data
elements such as character, integer and floats.
Ex: - @array = (“surendra”, 1, 12,”tux”);
$# operator gives the size of the array.
Ex: - $#array => 4
Accessing Arrays:-
With dollar operator along with array name and index number inside
square braces will give the array element.
Ex: - @array = (“surendra”, 1, 10,”patil”);
$arra [0]
=> Surendra
$array[2]
=> 10
Program to display
all the elements in an array:-
use strict;
use warnings;
#
# Print Array Elements
#
my @array =
("Surendra",10,111,"patil","tux");
my $i = undef;
for ($i = 0; $i < $#array; $i++) {
print "$array[$i]\n";
}
Output:-
Surendra
10
111
patil
tux
Multidimensional Arrays:-
use strict;
use warnings;
#
# Print Multi Dimention Array Elements
#
my @array = (
["Surendra",10],
["veerendra",31],
["chukchuk",9],
["santosh",8],
);
my $i = undef;
my $j = undef;
for ($i = 0; $i < $#array; $i++) { #Rows
for ($j = 0; $j
< 2; $j++) { #Columns
print
"$array[$i][$j]\t";
}
print
"\n";
}
Output:-
Surendra 10
veerendra 31
chukchuk 9
Comments