In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable.

Example:
@number = (50, 70, 46);
@names = ("Geeks", "For", "Geeks");
Array Creation: In Perl programming every array variable is declared using “@” sign before the variable’s name. A single array can also store elements of multiple datatypes. For Example:
# Define an array
@arr = (1, 2, 3);
@arr = (1, 2, 3, "Hello");
Array creation using qw function:
qw() function is the easiest way to create an array of single-quoted words. It takes an expression as an input and extracts the words separated by a whitespace and then returns a list of those words. The best thing is that the expression can be surrounded by any delimiter like- () ” [] {} // etc. However () and // are used generally.
Syntax:
qw (Expression)
qw /Expression/
qw 'Expression'
qw {Expression}
Example :
@arr1 = qw /This is a Perl Tutorial by GeeksforGeeks/;
@arr2 = @arr1 [2,3,4];
print "Elements of arr1 are:\n" ;
foreach $ele ( @arr1 )
{
print "$ele \n" ;
}
print "Elements of arr2 are:\n" ;
foreach $ele ( @arr2 )
{
print "$ele \n" ;
}
|
Output:
Elements of arr1 are:
This
is
a
Perl
Tutorial
by
GeeksforGeeks
Elements of arr2 are:
a
Perl
Tutorial
Accessing Array Elements: For accessing the elements of an array we must prefix “$” sign before the array variable name followed by the index in square brackets. For Example:
# Define an array
@arr = (1, 2, 3);
# Accessing and printing first
# element of an array
print "$arr[0]\n";
# Accessing and printing second
# element of an array
print "$arr[1]\n";
Example:
@fruits = ( "apple" , "banana" , "pineapple" , "kiwi" );
print "@fruits\n" ;
print "$fruits[0]\n" ;
print "$fruits[1]\n" ;
print "$fruits[2]\n" ;
print "$fruits[3]\n" ;
|
Output:
apple banana pineapple kiwi
apple
banana
pineapple
kiwi
Note: Array indices always start from zero. To access the first element it must to give 0 as indices. We can also give a negative index. But giving negative index will result in selecting the array elements from ending not from the beginning.
Example:
@fruits = ( "apple" , "banana" , "pineapple" , "kiwi" );
print "$fruits[-1]\n" ;
print "$fruits[-2]\n" ;
|
Output:
kiwi
pineapple
Sequential Number Arrays: Perl also provides a shortcut to make a sequential array of numbers or letters. It makes out the user’s task easy. Using sequential number arrays users can skip out loops and typing each element when counting to 1000 or letters A to Z etc.
Example:
@array = (1..9); # array with numbers from 1 to 9
@array = (a..h); # array with letters from a to h
Program:
@nums = (1..9);
@letters = (a..h);
print "@nums\n" ;
print "@letters\n" ;
|
Output:
1 2 3 4 5 6 7 8 9
a b c d e f g h
Size of an Array: The size of an array(physical size of the array) can be found by evaluating the array in scalar context. The returned value will be the number of elements in the array. An array can be evaluated in scalar context using two ways:
- Implicit Scalar Context
$size = @array;
- Explicit scalar context using keyword scalar
$size = scalar @array;
Both ways will produce the same output so it is preferred to use an implicit scalar context.
Example:
@arr = (11, 22, 33, 44, 55, 66);
$imp_size = @arr ;
$exp_size = scalar @arr ;
print "Size of arr(imp_size) $imp_size\n" ;
print "Size of arr(exp_size) $exp_size" ;
|
Output:
Size of arr(imp_size) 6
Size of arr(exp_size) 6
Note: In Perl arrays, the size of an array is always equal to (maximum_index + 1) i.e.
size = maximum_index + 1
And you can find the maximum index of array by using $#array. So @array and scalar @array is always used to find the size of an array.
Example:
@arr = (10, 17, 19, 20, 25);
$size_of_array = @arr ;
$maximum_index = $#arr ;
print "Maximum Index of the Array: $maximum_index\n" ;
print "The Size of the Array: $size_of_array\n" ;
|
Output:
Maximum Index of the Array: 4
The Size of the Array: 5
Iterating through an Array: We can iterate in an array using two ways: