in reply to combine elements of lists

For this example you could write:
my %list; $list[1]=[1,2,6]; $list[2]=[4,5]; $list[3]=[7,3]; my $n = 1; foreach my $A (@{$list[1]}) { foreach my $B (@{$list[2]}) { foreach my $C (@{$list[3]}) { print "comb[$n] = [$A,$B,$C]\n"; $n++; } } }
You now have to generalize this using a loop.

Replies are listed 'Best First'.
Re^2: combine elements of lists
by mscharrer (Hermit) on Apr 22, 2008 at 13:51 UTC
    I know implemented some general loop for your problem. A index array is used to remember which index of which array is currently taken. In every iteration the index of the first array is incremented and wrapped around to zero if got to big. In this case the second index is incremented, and so forth...

    The resulting order is combinations is here in different, but this could be fixed by changing the order of index incrementation.

    my @lists = ( [1,2,6], [4,5], [7,3], ); # Init indexes to zero my @idx = map { 0 } (0..$#lists); my $n = 0; COMP: while (1) { $n++; print "comp[$n] = ["; # Join all list elements given by index array print join ',', map { $lists[$_][$idx[$_]] } (0..$#lists); print "]\n"; # Increment lowest index, handle overrun INDEX: foreach my $i (0..$#lists) { $idx[$i]++; # If new index greater then highest index if ($idx[$i] > $#{$lists[$i]}) { # Wrap around $idx[$i] = 0; if ($i == $#lists) { # Stop outher loop if last index overruns last COMP; } } else { # stop if no overrun occured last INDEX; } } } __END__ comp[1] = [1,4,7] comp[2] = [2,4,7] comp[3] = [6,4,7] comp[4] = [1,5,7] comp[5] = [2,5,7] comp[6] = [6,5,7] comp[7] = [1,4,3] comp[8] = [2,4,3] comp[9] = [6,4,3] comp[10] = [1,5,3] comp[11] = [2,5,3] comp[12] = [6,5,3]
    Update: To get the same order like in your example add/change the following:
    22a23 > $i = $#lists - $i; 28c29 < if ($i == $#lists) { --- > if ($i == 0) {
    e.g. add the first line after the INDEX foreach loop line and change the if statement like shown.
Re^2: combine elements of lists
by alkis (Acolyte) on Apr 22, 2008 at 13:46 UTC
    mscharrer this is about something i have thought to do, but how can i generalize it? i dont know a priori how many lists will be created, it depends on the input...there might be 1 or 2 or 3 or more...