in reply to get substring when you know START and END???

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^2: get substring when you know START and END???
by swampyankee (Parson) on May 27, 2006 at 21:45 UTC

    Try a little arithmetic. The number of characters in a substring is (end - start) +1.

    Perl counts the leftmost position (as shown in my English language world) as position '0'. Presuming you want the string between positions 5 (counting from 0, that would be "V" in the string you show) and 10 (which, if I counted right) is "Y", you would be including "TNNA" (this is what you specified, but is it what you want?), which would be

    $sub = substr($seq,6, 4);

    If you want the characters in positions 5 through 10, you would write something like:

    $sub = substr($seq, 5, 6);

    which would (again, if I counted right) return "VTNNAY" -- the characters in positions 5 through 10.

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele
Re^2: get substring when you know START and END???
by johngg (Canon) on May 27, 2006 at 21:54 UTC
    If you want a substring from character 5 through character 10 including both then the substring length will be 10 - 5 + 1. The substr function counts offsets from zero so if you are counting from one you need to make an adjustment. The following code assumes you are counting like substr

    # # 1 2 3 # 01234567890123456789012345678901234 my $seq = "LPNTGVTNNAYMPLLGIIGLVTSFSLLGLXKARKD"; # # Note that substr counts offsets from zero so adjust # if you are counting from one. my $start = 5; my $end = 10; my $length = $end - $start + 1; my $cut = substr $seq, $start, $length; print "$cut\n";

    this prints

    VTNNAY

    Cheers,

    JohnGG

Re^2: get substring when you know START and END???
by GrandFather (Saint) on May 27, 2006 at 21:36 UTC

    What would you like to see printed? The two previous answers seem to have answered the question that you asked.

    It is not clear whether you mean characters numbered from 1 or 0, otherwise you have answers for just what you asked.


    DWIM is Perl's answer to Gödel
Re^2: get substring when you know START and END???
by sarani (Sexton) on May 28, 2006 at 06:21 UTC
    Exactly. Therefore, in your example, the second number will be 5.

    Print the substring that starts from position 5, go five char because
    (length = END-START).

    Logical?