How can I generate a list of words made up of combinations of three word lists in Perl?
- by Chris Denman
I have three lists of words. I would like to generate a single list of all the combinations of words from the three lists.
List 1:
red
green
blue
List 2:
one
two
List 3:
apple
banana
The final list would like like so:
red one apple
red two apple
red one banana
red two banana
... and so on
Ideally I'd like to pass in three arrays and the routine return one array.
I have done a simple loop like so:
foreach $word1 (@list1){
foreach $word2 (@list2){
foreach $word3 (@list3){
print "$word1 $word2 $word3\n";
}
}
}
However, this doesn't work if there's nothing in the second or third list (I may only want to iterate between one, two or three lists at a time - in other words, if I only supply two lists it should iterate between those two lists).