in reply to cartesian product preserving order

glob is perhaps the prettiest solution, but it isn't recommended for large sets, since all combinations will be generated before any are returned. A recursive solution is better - something like the following:
sub comb { my $s = shift; if (@_ == 1) { print "$s$_\n" for @{$_[0]}; return; } comb("$s$_",@_) for @{shift()}; } comb '',[qw(a b c)],[qw(1 2 3)],[qw(- + *)];
Where you can change this line to do whatever you want with the combination:
if (@_ == 1) { dowhatever("$s$_") for @{$_[0]}; return; }
BrowserUK's solution is somewhat neater, but it's more a pull down approach, where all combinations are returned at once, rather than a push up approach, where only one combination is worked on at a time. You might as well use glob if you're going that route.

Replies are listed 'Best First'.
Re^2: cartesian product preserving order
by ikegami (Patriarch) on Jun 09, 2005 at 05:54 UTC
    glob is perhaps the prettiest solution, but it isn't recommended for large sets, since all combinations will be generated before any are returned.

    So does the comb you wrote.

Re^2: cartesian product preserving order
by Limbic~Region (Chancellor) on Jun 09, 2005 at 12:48 UTC
    TedPride,
    ...but it isn't recommended for large sets, since all combinations will be generated before any are returned.

    What makes you think that? In scalar context, glob becomes an iterator. That isn't how it was used, but it could have been. How is a recursive solution any better - it builds the entire solution in memory before returning it.

    Cheers - L~R