jackdied has asked for the wisdom of the Perl Monks concerning the following question:
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 overkillpackage 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;
Suggestions?
-jackdied
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Name Me! MixMatch?
by ariels (Curate) on Dec 19, 2001 at 14:20 UTC | |
by jackdied (Monk) on Dec 19, 2001 at 14:31 UTC | |
by ariels (Curate) on Dec 19, 2001 at 14:38 UTC | |
Re (tilly) 1: Name Me! MixMatch?
by tilly (Archbishop) on Jan 02, 2002 at 04:40 UTC | |
Broken, except for trivail cases (such as the example)
by jackdied (Monk) on Dec 20, 2001 at 04:28 UTC | |
Re: Name Me! MixMatch?
by chip (Curate) on Dec 19, 2001 at 14:08 UTC | |
Re: Name Me! MixMatch?
by blakem (Monsignor) on Jan 15, 2002 at 05:26 UTC |