Cartesian product is a way to order sets such that I googled out various methods and couldn’t find satisfactory code; there are a few but not clearly explained. Heres one I came up with.
Ex: @a = {1 2} @b = {3 4 5}
we need {1,3},{1,4},{1,5},{2,3},{2,4},{2,5}
@a and @b are arrays that have the sets to be ordered.
@cartesian_product = &cartesian(\@a,\@b);
sub cartesian{
my ($a,$b) = @_;
my @c = ();
map {my $w=$_; push @c, map {“$w “.$_} @$b } @$a;
return @c;
}
Or, you could just use the Set::CrossProduct module that does it for an arbitrary number of sets without creating everything in memory (and without abusing map in void context).
well that depends on how much you need; to download an entire package for a piece of code ; is that really required? my snippet can handle large sets on a contemporary machine.