- 论坛徽章:
- 46
|
use strict;
use warnings;
package Perm;
sub new {
my $class = shift;
my $prod = 1;
$prod *= @$_ for @_;
bless {
data => [@_],
idx => -1,
size => [map scalar @$_, @_],
max => $prod,
}, $class;
}
sub _next {
my $self = shift;
$self->{idx}++;
return 0 if $self->{idx} >= $self->{max};
my @idx = _select($self->{size}, $self->{idx});
my $data = $self->{data};
[map $data->[$_]->[$idx[$_]], 0 .. $#idx];
}
sub _select {
my($size, $idx) = @_;
my @result;
for (reverse @$size) {
push @result, $idx % $_;
$idx = int($idx / $_);
}
return reverse @result;
}
package main;
my $t = Perm->new(['a3', 'jj', 'm@', 'cc'], ['===>'], [1, 2 ,3, 4], [qw/@ $ %/]);
while ($_ = $t->_next) {
print "@$_\n";
} |
|