foreach my $con ( @consonants ) {
foreach my $vow ( @vowels ) {
..
}
}
####
foreach my $con_1 ( @consonants ) {
foreach my $vow_1 ( @vowels ) {
foreach my $con_2 ( @consonants ) {
foreach my $con_3 ( @consonants ) {
foreach my $vow_2 ( @vowels ) {
foreach my $con_4 ( @consonants ) {
...
}
}
}
}
}
}
####
#!/usr/bin/perl
my $template = shift || die "usage: $0 \n";
my @pattern = split( //, $template );
my $levels = scalar( @pattern );
my $vowels = [ qw( a e i o u y ) ];
my $consonants = [ qw( b c d f g h j k l m n p q r s t v w x z ) ];
output_cv( 0, \@pattern, $consonants, $vowels, "" );
sub output_cv {
my( $level, $pattern, $consonants, $vowels, $string ) = @_;
# if we have no more levels in the pattern, output the buffered string
if( $level == scalar( @$pattern ) ) {
print "$string\n";
} else {
# figure out which array at this pattern level
my $array = $pattern->[$level] eq 'C' ? $consonants : $vowels;
foreach my $ele ( @$array ) {
# start new buffer string
my $new_string = $string . $ele;
# recurse to next level
my $new_level = $level + 1;
output_cv( $new_level, $pattern, $consonants, $vowels, $new_string );
}
}
}