Here is a module I use for solving little problems (it's in my Dr Dobbs toolkit) and I need a better name. Someday I might throw it in with my Combination module and Permutation module (both of which I also use for Dr Dobbs type problems), and then I'll need to figure out a namespace. For now, I just need to name this little guy.
package MixMatch;
use strict;
=comments
Takes an lref of lrefs like
[
[a],
[x, y, z]
[1, 2]
]
and returns in order
[a, x, 1]
[a, x, 2]
[a, y, 1]
[a, y, 2]
[a, z, 1]
[a, z, 2]
=cut
sub new {
my $self = bless {}, shift;
my %args = (-array=>undef, # lref of lrefs
@_);
$self->{arr} = $args{-array};
my $sizes = [];
my $total = 1;
foreach my $lref (@{$self->{arr}}) {
push @$sizes, scalar(@$lref);
$total *= $sizes->[-1];
}
$self->{rows} = scalar(@{$self->{arr}});
$self->{sizes} = $sizes;
$self->{count} = $total;
return $self;
}
sub next {
my $self = shift;
my $c = $self->{count}--;
return undef unless $c;
my $ret = [];
for (my $i = 0; $i < $self->{rows}; $i++) {
push @$ret, $self->{arr}->[$i]->[$c % $self->{sizes}->[$i]];
}
return $ret;
}
1;
I won't post my Permutation.pm here, it is 4x slower than Algorithm::Permute (which is a nice piece of XS), I just use mine because I like the order of permutations is produces. And yes, this module could be implemented in terms of Permute's, but it is so short that new()ing a few other objects seems like overkill
Suggestions?
-jackdied