Hi.

You asked for 'a quick way' to do what you want. If, by any chance, by 'quick' you mean 'fast_to_execute' rather than 'fast_to_program', you might consider the far from elegant code below.

If you have a significant volume of data to process (and, judging by your questions here on PM, you CatGat guys generally do :), the code below _should_ be faster than any of the replies already posted because:

1) It doesn't use the perl regex engine

2) It works directly at string level, without introducing the overhead of converting strings into arrays and back again.

use strict; use warnings; my $str = 'A[ACGT]G[TG]G[ACT]C[AT]'; # or whatever my $strings = [ $str ]; while ( index ( $$strings[0], '[' ) > -1 ) { $strings = expand ( $strings ) ; } print "$_\n" for @$strings; sub expand { my $arg = shift; my @to_expand = @$arg; my $idx1 = index $to_expand[0], '['; my $idx2 = index $to_expand[0], ']'; my $post = substr $to_expand[0], $idx2 + 1; my @expanded; foreach my $item ( @to_expand ) { my $pre = substr $item, 0, $idx1; for ( $idx1 + 1 .. $idx2 - 1 ) { push @expanded, $pre . substr ( $item, $_, 1 ) . $post; } } return \@expanded; }

Disclaimer: only partially tested...


In reply to Re: Breaking Up a Bracketed String by Not_a_Number
in thread Breaking Up a Bracketed String by monkfan

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.