To iterate over an array in Perl, you can use foreach
loops or array index iteration.
In this example,
@numbers
with some values.foreach
loop to iterate over each element of the array and print the elements.@numbers = (1, 2, 3, 4, 5);
foreach my $num (@numbers) {
print "$num\n";
}
1 2 3 4 5
In this example,
@fruits
with some values.@fruits = ("apple", "banana", "cherry");
for my $i (0 .. $#fruits) {
print "$fruits[$i]\n";
}
apple banana cherry
In this tutorial, we learned How to Iterate Over an Array in Perl language with well detailed examples.