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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.