@seqarray and $seqarray are two different variables, and you never assign anything to $seqarray, so using substr on it does not make much sense, I suspect you just want to look directly at $seq instead of splitting it (BTW, to get multiple elements out of an array, use Slices or splice). Also, note that you overwrite $amino_acid on every loop iteration. The following minimal changes make your code work for me:

my $seq = shift; my $amino_acid; for (my $i=0; $i<=length($seq)-3; $i=$i+3) { my $codon = substr($seq,$i,3); $amino_acid .= $genetic_code{$codon}; } return $amino_acid;

<update2> Fixed an off-by-one error in the above code; I initially incorrectly translated your $#seqarray-2 into length($seq)-2 ($#seqarray returns the last index of the array, not its length like scalar(@seqarray) does, or length does for strings). That's a good argument against the classic for(;;) and for the two solutions below instead :-) </update2>

If you output the return value from OneFrameTranslation (your current code is ignoring the return value), this gives you:

print OneFrameTranslation('ATGCCCGTAC'),"\n"; print OneFrameTranslation('GCTTCCCAGCGC'),"\n"; __END__ MPV ASQR

By the way, you can probably move your %genetic_code to the top of your code (outside of the sub), so that it only gets initialized once instead of on every call to the sub, and making its name uppercase is the usual convention to indicate it is a constant that should not be changed.

Another way to break up a string is using regular expressions, the following also works - it matches three characters, and then matches again at the position that the previous match finished, and so on:

my $amino_acid; while ($seq=~/\G(...)/sg) { $amino_acid .= $genetic_code{$1}; } return $amino_acid;

Or, possibly going a little overboard, here's a technique I describe in Building Regex Alternations Dynamically to make the replacements using a single regex. I have left out the quotemeta and sort steps only because I know for certain that all keys are three-character strings without any special characters, if you have any doubts about the input data, put those steps back in!

# build the regex, this only needs to be done once my ($genetic_regex) = map qr/$_/, join '|', keys %genetic_code; # apply the regex (my $amino_acid = $seq) =~ s/($genetic_regex)/$genetic_code{$1}/g; return $amino_acid;

However, note this produces slightly different output for the first input: "MPVC" (the leftover C remains unchanged). Whether or not you want this behavior or not is up to you; it can also be accomplished in the first two solutions (although slightly less elegantly than with a regex). Update: Also, in the first two solutions you haven't defined what would happen if a code happens to not be available in the table; the third regex solution would simply leave it unchanged. Also minor edits for clarification.


In reply to Re: Translation Substring Error (updated) by haukex
in thread Translation Substring Error by FIJI42

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.