in reply to Re^2: Using Recursion to Find DNA Sequences
in thread Using Recursion to Find DNA Sequences
When I got deep recursion, I tried changing my regex, but I see that I just made it match the entire string. Is the problem still with my regex or am I using recursion incorrectly?
I don't see any need for recursion at all. As far as I can see, everything you want to do can be done in a properly constituted regex. There may be speed considerations that make a different approach preferable, but they can only be addressed by careful specification and benchmarking.
Update: To directly address your question about recursion:
When you find any match of $seq against the regex, you take the matching subsequence $1 and feed it back to the function again:sub find_coding { my ( $seq ) = @_; while ( $seq =~ /($regex)/g ) { my $match = $1; my $sequence = find_coding($match); my $allmatch = find_coding($sequence); print "Coding region: $allmatch", "\n"; } }
Note also that the
my $sequence = find_coding($match);
statement and the following
my $allmatch = find_coding($sequence);
statement (were it ever to be executed) are meaningless because the find_coding() function doesn't return anything meaningful; as best I can see, it would return (if it ever did) the return value of the print statement at the end of the while-loop. (Update: Changed this paragraph to address both statements.)
Update:
...the find_coding() function ... would return ... the return value of the print statement at the end of the while-loop.On second thought, I take this back. Without an explicit return statement, a function returns the value of the last expression executed in the function. My idea was that the print statement at the end of the while-loop would be that last expression. In fact, if the function didn't infinitely recurse, the last expression executed in the function would be the $seq =~ /($regex)/g regex in the conditional of the while-loop: when this expression evaluates false, the loop will exit and, as there is no code in the function after the loop, the function will exit and return this ever-false value.
Give a man a fish: <%-{-{-{-<
|
|---|