Felix Geerinckx wrote at Mon, 08 Jul 2002 14:52:14 +0200:
Or in more general we are talking about cross products of some
> Why not use nested for-loops:
>
> #! perl -w
> use strict;
>
> my @types = qw(basketball baseball beach golfball soccerball);
> my @sizes = qw(large medium small);
> my @colors = qw(green red);
>
> my @fullnames = generate_fullnames(\@types, \@sizes, \@colors);
> print "$_\n" for (@fullnames);
>
> sub generate_fullnames {
> my ($typesref, $sizesref, $colorsref) = @_;
> my @fullnames = ();
>
> for my $t (@$typesref) {
> for my $s (@$sizesref) {
> for my $c (@$colorsref) {
> push @fullnames, "a $c, $s $t";
> }
> }
> }
> return @fullnames;
> }
> }
What could be rewritten as:
use Set::CrossProduct; # from CPAN
my @colors = qw/green red/;
my @sizes = qw/large medium small/;
my @types = qw/basketball baseball beach golfball soccerball/;
my $things = Set::CrossProduct->new( [ \@colors, \@sizes, \@types ] );
And then either
print map "a $_->[0], $_->[1] $_->[2]\n", $things->combinations();
or
while (my (color, $size, $type) = $things->get()) {
print "a $color, $size $type\n";
}
Cheerio,
Janek
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]