note
fglock
<p>If you want to generate all possible combinations, you
could use this algorithm:</p>
<code>$categories = 5;
$columns = 3;
$a = join('#', 0..$categories);
@columns = ();
&check_columns ($a, @columns);
sub check_columns {
my $cat = shift;
my @columns = @_;
my $size = split('#', $cat)-1;
foreach (1.. $size ) {
my ($first, $second) = $cat =~ /^((?:.*?#){$_})(.*?)$/;
$first =~ s/#$//;
my @result = ($first, $second, @columns);
if ($#result == $columns) {
print join(" ", @result), "\n",
}
else {
check_columns(@result);
}
}
}</code>
<p>It generates this list of indexes, from
which you can choose the best
line:</p>
<code>0 1 2 3#4#5
0 1 2#3 4#5
0 1#2 3 4#5
0#1 2 3 4#5
0 1 2#3#4 5
0 1#2 3#4 5
0#1 2 3#4 5
0 1#2#3 4 5
0#1 2#3 4 5
0#1#2 3 4 5</code>
<p>I used an array of strings instead of an array of
arrays because it is easier to debug.</p>
180276
180276