To iterate over a Two Dimensional Array in Perl, you can use nested loops or the foreach loop.
In this example,
@matrix
.@matrix = ([1, 2], [3, 4], [5, 6]);
foreach my $row (@matrix) {
foreach my $element (@$row) {
print "$element ";
}
print "\n";
}
1 2 3 4 5 6
In this example,
@matrix
.foreach
loop to iterate over each row of the array.foreach
loop to iterate over each element of the row and print the element.@matrix = ([1, 2], [3, 4], [5, 6]);
foreach my $row (@matrix) {
foreach my $element (@$row) {
print "$element ";
}
print "\n";
}
1 2 3 4 5 6
In this tutorial, we learned How to Iterate over a Two Dimensional Array in Perl language with well detailed examples.