in reply to Trying to construct all possible strings from 4 bases [ATCG]
What's wrong with my code below ?You appear to be trying to use what I think of as a "meta-for loop" to get the effect of $l nested for loops. No programming language I know of implements this directly.
In a language that implemented the "meta-for" construct you could write something likefor ( my $i = 0 ; $i < $l ; $i++ ) { foreach my $nucl (@nucleotides) { substr( $cand_motif, $i, 1 ) = $nucl; push @enumerated_motifs, $cand_motif; #print "$cand_motif\n"; } }
Of course, parsing the above code as written presents serious difficulties. Probably a different syntax would be required.meta_for ( my $i = 0 ; $i < $l ; $i++ ) { foreach my $nucl (@nucleotides) { substr( $cand_motif, $i, 1 ) = $nucl; } # meta_for push @enumerated_motifs, $cand_motif; #print "$cand_motif\n"; meta_end_for ( my $i = 0 ; $i < $l ; $i++ ) { } # foreach } # meta_end_for
Update: Probably the best way to implement this (Update2: i.e. the "meta-for" construct) in Perl would be to use eval.
my $code = "xyzzy"; for ( my $i = 0 ; $i < $l ; $i++ ) { $code =~ s/xyzzy/ foreach my \$nucl (\@nucleotides) { substr( \$cand_motif, $i, 1 ) = \$nucl; xyzzy } /; } $code =~ s/xyzzy/ push \@enumerated_motifs, \$cand_motif; #print "\$cand_motif\\n"; /; eval $code;
|
|---|