Search⌘ K
AI Features

Loops Within Loops

Explore nested loops in Perl to handle complex control flow. Learn to declare iteration variables clearly and avoid common mistakes with filehandles by applying practical solutions for maintainable and efficient code.

We'll cover the following...

Nested loops

We can nest loops within other loops:

Perl
my @suits = ('Hearts', 'Diamonds', 'Clubs', 'Spades');
my @card_values = ('Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two');
for my $suit (@suits) {
for my $values (@card_values) {
# inner loop body
say "$values of $suit";
}
}

Note the value of declaring iteration variables! The potential for ...