in reply to Breaking Up a Bracketed String
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...
|
|---|