in reply to Combinations of an array of arrays...?
Here's one way to do it, using the Algorithm::Loops module:
use strict; use Algorithm::Loops qw/ NestedLoops /; my @arrays = ( [qw/ a b c /], [qw/ d e /], [qw/ f g h /], [qw/ i /], [qw/ j k /], [qw/ l /], [qw/ m /], ); NestedLoops( [ map [ undef, @$_ ], @arrays ], sub { my $count = 0; my $string = join '', grep { defined && ++$count } @_; print $string if $count >= 4; }, );
First, this combines the 7 individually named arrays into a single array of arrays to make it easier to handle them as a set.
The first parameter to NestedLoops() constructs the sets over which to loop by taking each of the 7 arrays and including undef to represent "nothing chosen from this array".
The second parameter is the code executed for each selection, which counts the number of defined values (to make sure we have at least 4), joins those defined values into a single string, and prints them.
Note that some slight rearrangement would allow you to use this to construct an iterator which would return the next valid combination each time you call it; that would probably be the more useful approach if you want to do something other than simply print them.
Hugo
|
|---|