How can I generate a list of words made up of combinations of three word lists in Perl?
Posted
by Chris Denman
on Stack Overflow
See other posts from Stack Overflow
or by Chris Denman
Published on 2010-04-13T22:18:02Z
Indexed on
2010/04/13
22:23 UTC
Read the original article
Hit count: 262
perl
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).
© Stack Overflow or respective owner