Combinations are distinct subsets of a set. So I am not sure what the Python snippet is really meant to produce. If I were asked to produce all combinations of words in a string, I'd use the theory that the number of subsets of a set of N members is 2**N. The reason is that a bit-string of N bits, where each bit represents inclusion of an element, represents a combination and all possible combinations can therefore be represented by all possible numbers expressed as these bit strings from 0 to (2**N)-1 (of which there are 2**N, including the empty set). So converting the theory into Perl:-

Update: I just noticed the OP wants to vary separators not words, so I'll address that further down...

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"; }
which produces the following output:
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

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 '-'; } } }
producing
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.