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;