my @superset = split /\s+/, 'convert python to perl'; my $N = scalar @superset; for my $combo (0..((2**$N) - 1)) { # a combo is also a bit mask print "selection:"; for my $selector (0..($N-1)) { # a selector is also a bit (2**$selector) & $combo and print ' ' . $superset[$selector]; } print "\n"; } #### selection: selection: convert selection: python selection: convert python selection: to selection: convert to selection: python to selection: convert python to selection: perl selection: convert perl selection: python perl selection: convert python perl selection: to perl selection: convert to perl selection: python to perl selection: convert python to perl #### my @superset = split /\s+/, 'convert python to perl'; my $N = $#superset; # one less element than in the previous example for my $combo (0..((2**$N) - 1)) { # a combo is a bit mask for my $selector (0..$N) { # a selector is also a bit position print $superset[$selector]; if ($selector == $N) { print "\n"; next; } if ((2**$selector) & $combo) { print ' '; } else { print '-'; } } } #### convert-python-to-perl convert python-to-perl convert-python to-perl convert python to-perl convert-python-to perl convert python-to perl convert-python to perl convert python to perl