in reply to array logic

There are probably many ways to do this. Chances are there is some whizzy one-liner using Quantum::Superpositions or some such - but I haven't had time to look.

There are 2n-1 possibilities (where n is the length of your input array) and that suggested (to me) looking at the binary digits of each number from 1 to 2n-1. That's what this solution does. It probably isn't the most elegant, and I suspect it is not the most efficient either.

use strict; use Data::Dumper; my @firstArray = ( "item1", "item2", "item3" ); my $n = @firstArray; my @results; for my $i (1..2**$n-1) { my @selection; for my $j (0..$n-1) { if ($i & 2**$j) { push @selection, $firstArray[$j]; } } push @results, \@selection; } print Dumper(\@results);

s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&

Replies are listed 'Best First'.
Re^2: array logic
by rsiedl (Friar) on Sep 02, 2004 at 11:06 UTC
    Thanks muntfish. Thats perfect for what i need. Cheers.