Loops Within Loops

Learn how to implement nested loops in Perl.

Nested loops

We can nest loops within other loops:

Press + to interact
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 ...

Get hands-on with 1400+ tech skills courses.