Perl offers scalar
types of variables that store single values.
For storing multiple values with a single variable, Arrays
and List
are used.
Arrays are a special type of variable that holds multiple values.
Array variables are declared with @
followed by the variable name.
@numbers = ("one", "two", "three");
Each element is a scalar value type in an array
How to create an array in Perl
Array Variables can be created using @symbol.
Array initiated with data in multiple ways
- using
()
symbol Variables are declared with @variable and initialized with a group of values, separated by commas enclosed in()
.
@numbers = ("one", "two", "three");
- use qw operator
qa operator followed by
/
and a list of values ending with/
. This operator parses the string, delimited by space.
@strings = qw/another way to declare variable/;
- using sequential arrays
It is another shortcut way to assign sequential numbers and letters to the array. Syntax
@array1 = (start..end);
The start and end are numbers or letters. It contains ..
and is enclosed in ()
@array1 = (10..20);
@array2 = (b..k);
print "@array1\n";
print "@array2\n";
with printing an array in Perl, output:
10 11 12 13 14 15 16 17 18 19 20
b c d e f g h i j k
How to access the elements in an array?
Once you declared an array, elements can be accessed with a variable prefixed with $
using the below syntax.
$arrayvariable[index]
index
starts from zero until the length of a string minus 1.
Here is an example
@numbers = ("one", "two", "three");
print($numbers[0]); # one
print($numbers[1]); # two
print($numbers[2]); # three
Another example
@strings = qw/another way to declare variable/;
print($strings[0]); # another
print($strings[1]); # way
print($strings[2]); # to
print($strings[3]); # declare
print($strings[4]); # variable
Find the size of an array in Perl.
To get a size of an array, Please follow multiple ways
- using scalar keyword
@array1 = (10..20);
@array2 = (b..k);
print "@array1\n";
print "@array2\n";
print scalar @array1,"\n";#11
print scalar @array2;#10
- using default variable syntax
Perl default variable($#
) returns the last index of an array.
Add 1 to this result to get the size of an array
@array1 = (1,2,3,5);
@array2 = (b..k);
print $#array1,"\n"; #3
print $#array2; #9
how to iterate an array of elements in Perl?
To iterate elements an array, use for loop
@array1 = (1,2,3,4,5);
for my $item (@array1) {
print $item,"\n";
}
output:
1
2
3
4
5
@array1 = (1,2,3,4,5);
for my $i (0 .. $#array1) {
print $i,"-",$array1[$i],"\n";
}
output:
0-1
1-2
2-3
3-4
4-5
Perl Array Functions
Perl provides various Inbuilt functions to add or remove elements from an array. Below operators mutates an array.
- Left Side processing operator
- Shift
- unshift
- Right side Proessing operator
- pop
- push : adds an element to an array at end.
- splice
Perl Shift Example
Perl Push example
Arrays
can be added with below syntax array[index] if we know the index.
@numbers = ("1", "2","3");
$numbers[3] = "4";
print "@numbers"; # 1 2 3 4
Another way to add using push function in perl.
push
function adds an element to an end of an array.
Syntax:
push(array, element)
Here, element is added to end of an array, returns an element. Here is an example how to add an element to an array.
@numbers = ("1", "2","3");
push(@numbers, "4");
print "@numbers"; # 1 2 3 4
Perl pop example
pop function removes last element from an array. It is an opposite of push function. It mutates an array.
pop(array)
Last element removed from an array, returns an element. Here is an example how to remove an element from an array.
@numbers = ("1", "2","3");
$result = pop(@numbers);
print "$result\n"; # 3
print "@numbers\n"; #1 2