in reply to 2D array ALL combinations

Algorithm::Loops's NestedLoops would handle this nicely.

use Algorithm::Loops qw( NestedLoops ); my @AoA1 = ( [ 'a', 'b', 'c' ], [ 'd', 'e', '' ], [ 'f', 'g', '' ], [ 'h', '', '' ], ); my @AoA2 = NestedLoops(\@AoA1, sub { [ @_ ] } );

To use less memory:

use Algorithm::Loops qw( NestedLoops ); my @AoA1 = ( [ 'a', 'b', 'c' ], [ 'd', 'e', '' ], [ 'f', 'g', '' ], [ 'h', '', '' ], ); my @AoA2; my $iter = NestedLoops(\@AoA1); my @list; while (@list = $iter->()) { push @AoA2, [ @list ]; }

By the way, since you have two identical values in @{$AoA1[3]}, @AoA2 will contain many duplicates.

Untested.

Update: The above don't produce the results in the order you described. If the order matters, change

Replies are listed 'Best First'.
Re^2: 2D array ALL combinations
by Anonymous Monk on Jun 13, 2007 at 18:01 UTC
    ikegami,

    The elements '' (AoA1[1][2], AoA1[2][2], AoA1[3][1] and AoA1[3][2]) refer to empty elements. I would like combinations to exclude the empty ones to be more productive (my AoA1 is big).

    Thanks for hints, best.

      You could make a copy of AoA1 with them grepped out.

      my @cleaned_AoA1 = map { grep length, @$_ } @AoA1;

      If you don't mind being destructive, you could remove the blank elements from @AoA1.

      foreach (@AoA1) { @$_ = grep length, @$_; }

      I'm afraid that's the best NestedLoops can do because it can't loop using iterators.

      Grepping them out on the fly won't save you any memory.

      sub make_scrubber { my ($aref) = @_; return sub { [ grep length, @$aref ] }; } my $iter = NestedLoops( [ map { make_scrubber($_) } @AoA1 ] );