FFRANK has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Given a 2D array, in wich rows may have a different number of defined elements. How to store, in a second table, all possible combinations of row1 elements with row 2 elements with row 3 elements, etc. (i.e. 1 element per row).

Given:

my @AoA1 = (['a','b','c'], ['d','e',''], ['f','g',''], ['h','','']);
Put combinations into AoA2:
@row1 = qw(a d f h); @row2 = qw(b d f h); @row2 = qw(c d f h); @row2 = qw(a e f h);
and so on...

Thanks, regards.

Replies are listed 'Best First'.
Re: 2D array ALL combinations
by ikegami (Patriarch) on Jun 13, 2007 at 17:30 UTC

    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

    • \@AoA1 to [ reverse @AoA1 ],
    • [ @_ ] to [ reverse @_ ], and
    • [ @list ] to [ reverse @list ].
      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 ] );
Re: 2D array ALL combinations
by kyle (Abbot) on Jun 13, 2007 at 17:56 UTC

    It doesn't get the rows in the order that you're looking for, but it seems to get the results otherwise.

    my @aoa1 = ( [ 'a', 'b', 'c' ], [ 'd', 'e', '' ], [ 'f', 'g', '' ], [ 'h', '', '' ] ); sub combinations { return if ! @_; my @array = grep { $_ ne '' } @{shift()}; my @subs = combinations( @_ ); if ( ! @subs ) { return map { [ $_ ] } @array; } my @out; foreach my $item ( @array ) { foreach my $sub ( @subs ) { push @out, [ $item, @$sub ]; } } return @out; } my @aoa2 = combinations( @aoa1 );
Re: 2D array ALL combinations
by GrandFather (Saint) on Jun 13, 2007 at 21:11 UTC

    Why? There may be a better way of solving your larger problem. This sounds like a very expensive solution!


    DWIM is Perl's answer to Gödel