#!/usr/bin/perl -w use strict; my @one = qw{C A T}; my @two = qw{D O G}; my @three = qw{B I R D}; my @combos = permute(\@one, \@two, \@three); show_text(\@combos, \@one, \@two, \@three); sub permute { my @arrays = @_; my @lengths; foreach my $array_ref (@arrays) { push @lengths, scalar @$array_ref; } return combine(@lengths); } sub combine { my $length = shift; my @results; for (0 .. ($length - 1)) { if (@_) { foreach my $result (combine(@_)) { push @results, $_ . $result; } } else { push @results, $_; } } return @results; } sub show_text { my ($combos, @arrays) = @_; foreach my $combo (@$combos) { my $i = 0; my $text = ''; foreach my $elem (split'', $combo) { $text .= $arrays[$i++]->[$elem]; } print "$text\n"; } }