Hi, Monks.

I have a problem finding all permutations of a string. For example given {a|b}cd{f|g|h} i must get:

acdf
bcdf
acdg
...
bcdh

The main problem for me here is that original string may have unknown number of token {} (here we have only two but sometimes it's seven).

Here is my code (but it doesn't produce output i need):

#!/usr/bin/perl -w use strict; use warnings; my $input_text; open my $input_fh, "input.txt" or die $!; { local $/; $input_text = <$input_fh>; } close $input_fh; my @tokens; my $number = 1; while ( $input_text =~ m/\{([^\}]+)}/isxg ) { push @tokens, { values => [ split /\|/, $1 ], name => "%TOKEN_$number%", }; $input_text =~ s/\{([^\}]+)}/%TOKEN_$number%/is; $number++; } foreach my $token_id ( 1 .. scalar(@tokens) ) { foreach my $value ( @{ $tokens[ $token_id - 1 ]->{values} } ) { my $output_text = $input_text; $output_text =~ s/%TOKEN_$token_id%/$value/is; while ( $output_text =~ m{%TOKEN_(\d+)}is ) { my $id = $1; $output_text =~ s/%TOKEN_$id%/$tokens[ $id - 1 ]->{values} +->[0]/is; } print $output_text; #sleep 1; } } sleep 1;

In reply to Find all string permutations by Gangabass

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.