in reply to [SOLVED] Keeping track of substituted regions with a while loop

Based on your sample data, this seems more straightforward, and avoids the issue of substituting something that was already translated. Caveat: I'm guessing based on the sample data, so I may have missed something crucial.

my $protein; # split $rna into groups of 3 characters for ( unpack("(A3)*",$rna)) { if (exists($gencode{$_}) { $protein.=$gencode{$_}; } else { die "No mapping for [$_]\n"; } }

Replies are listed 'Best First'.
Re^2: Keeping track of substituted regions with a while loop
by kschwab (Vicar) on Nov 15, 2013 at 22:59 UTC
    Missed a paren...
    for ( unpack("(A3)*",$rna)) { if (exists($gencode{$_})) { $protein.=$gencode{$_}; } else { die "No mapping for [$_]\n"; } }
Re^2: Keeping track of substituted regions with a while loop
by enderk (Novice) on Nov 16, 2013 at 13:21 UTC

    Hi kschwab, thanks for your reply. You're absolutely right, that is another way of solving this problem (by sequentially appending the one-letter codes to a new string instead of substituting an existing string) and it is similar to the "alternative" method I described in my original post.

    However, I was not looking so much into getting the right answer as rather trying to learn more about how to use regexes, combined with a while loop, in this situation. Is there an "easy" way for Perl to keep track of the position it is in, as it moves along a string in a while loop, or would that be an overkill to do, considering there are easier options available?