Update: I just noticed the OP wants to vary separators not words, so I'll address that further down...
which produces the following output: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
Update: to vary spaces and hyphens (what I now understand is wanted), the same technique can apply
producingmy @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
More update: Afterthought: 1 << $var is likely to be faster than, while being equivalent to: 2 ** $var.
One world, one people
In reply to Re: Python to perl: itertools product to replace a char in string in each possible position
by anonymized user 468275
in thread Python to perl: itertools product to replace a char in string in each possible position
by danj35
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |