in reply to Breaking Up a Bracketed String

Just because I also had a crack at it (and got the solutions requested) I thought I'd publish my long-winded solution.

use strict; sub array_from_component { my ( $component, $index ) = @_; return( "" ) if ( scalar( @{$component} ) <= $index ); my @result = (); my @answer = array_from_component( $component, $index + 1 ); if ( $component->[$index]->[1] ) { # go through each letter one at a time foreach my $char ( split( //, $component->[$index]->[0] ) ) { foreach ( @answer ) { push( @result, $char . $_ ); } } } else { foreach ( @answer ) { push( @result, $component->[$index]->[0] . $_ ); } } return( @result ); } sub array_from_string( $ ) { my ( $str ) = @_; # split up string into components my @component = (); while ( $str =~ m/(\[)?([ATCG]+)(\])?/g ) { # $1 will be set if this was a bracketed string push( @component, [ $2, $1 ? 1 : 0 ] ); } # component now holds all the components, # now for some recursive fun my @answer = array_from_component( \@component, 0 ); return( @answer ); } # From my $str2 = "ATC[TG]CC"; my @result = array_from_string( $str2 ); print( join( "\n", @result ) );