Closed Thread
Results 1 to 10 of 10

Thread: need help

  1. #1
    lichy is offline Newbie
    Join Date
    Jun 2007
    Posts
    17
    Rep Power
    0

    need help

    for example:

    my input is "HCHATCABCKDLOTSHCNAGWABCHSTABCADLOS"

    and i would like to detect the substring "ABC" and make my output into

    "HCHATC(ABC)KDLOTSCNAGW(ABC)HST(ABC)ADLOS"

    and in addition, be able to detect the position of the substring.

    by using

    $input=<STDIN>; #my input
    $substring=<STDIN>; #my substring

    how am i supposed to do the following above using loop to scan through the whole string detecting every single substring?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    You can use the function substr.
    Take a look at this.

  4. #3
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    You don't need to use a loop. You can use a regexp to find the substring and place the () in the string around the substring. You can use index() to find the location of the substring in the string.

    Perl regular expessions tutorial

    index: find a substring within a string

  5. #4
    lichy is offline Newbie
    Join Date
    Jun 2007
    Posts
    17
    Rep Power
    0
    thanks guys, but for example

    input is TTTTTTTTTTTTTTTTTTTABCTTTTTTTABCTTTTTTT


    given that ABC is the phase of interest.

    final output is


    TTTTTTTTTTTTTTTT(ABC)TTTTTT(ABC)TTTTTTT
    (printed out)

    how to print the final output including those T's?

  6. #5
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    Didn't bother to read the tutorial I posted a link to? I will give you a solution this time, but I prefer to help people that show more effort than you have so far:

    one way:

    Code:
    my $string = "TTTTTTTTTTTTTTTTTTTABCTTTTTTTABCTTTTTTT";
    $string =~ s/([^T]+)/($1)/g;
    print $string;
    another way:

    Code:
    my $string = "HCHATCABCKDLOTSHCNAGWABCHSTABCADLOS";
    $string =~ s/(ABC)/($1)/g;
    print $string;
    next time post some perl code you have tried.

  7. #6
    lichy is offline Newbie
    Join Date
    Jun 2007
    Posts
    17
    Rep Power
    0
    thanks for the help.

    another query is when for example

    WHTSABCWHNSNDYCNSYUABCHDNABC
    _____4_______________19_____25
    i want the know the position of all ABC lies in a string.
    the result for the following position is(with the first character counted as 0)

    4,19 and 25.

    how am i supposed to do so? i can only get the first ABC position in the string, the rest of the ABC substring has been ignored.
    Last edited by lichy; 07-04-2007 at 07:25 PM.

  8. #7
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    see the link to the index function I posted above. From now on I will not help you unless you post some perl code that you have written and need help with.

  9. #8
    lichy is offline Newbie
    Join Date
    Jun 2007
    Posts
    17
    Rep Power
    0
    Quote Originally Posted by KevinADC View Post
    see the link to the index function I posted above. From now on I will not help you unless you post some perl code that you have written and need help with.
    sorry to say but your index link doesnt explain much on how to capture all the position of my desired substring in a string. this is my program anyway.

    $readingframe =1;

    for ($i=0;$i<$readingframe;$i++)
    {
    {

    $len = int(length(substr($sequence1, $i))/1);
    for ($j=0;$j<$len;$j++)

    {
    $nuc = substr($sequence1, $i+$j*1,1);
    if($nuc==$intseq)
    {

    $position = index($sequence1, $nuc);#the problem i am facing.




    $arry_pos[$j]=$position;
    @ary[$j]=$arry_pos[$j];
    }


    print "End of search.\n";
    }
    }



    print "\nPosition at: ";

    for ($j=0;$j<$len;$j++)

    {


    print "@ary[$j],";

    }


    in the end the output printed by @ary[$j], are "4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4" which is the first ABC position in the string.
    Last edited by lichy; 07-04-2007 at 08:00 PM.

  10. #9
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    It's true, the index() function does not tell you how to get the position of all the substrings within the string. But it is a trivial task once you understand how to apply some basic logic to the problem:

    Code:
    my $string = 'WHTSABCWHNSNDYCNSYUABCHDNABC';
    my $substring = 'ABC';
    my $r = length($string);
    my $n = length($substring);
    my $x = $r - $n;
    my $index = 0;
    do {
       $pos = index $string,$substring,$index;
       print "$substring is at position: $pos\n";
       $index = $pos+$n;
    } until ($pos >= $x);
    same thing using a "while" loop:

    Code:
    my $string = 'WHTSABCWHNSNDYCNSYUABCHDNABC';
    my $substring = 'ABC';
    my $r = length($string);
    my $n = length($substring);
    my $x = $r - $n;
    my $index = 0;
    while ($pos < $x) {
       $pos = index $string,$substring,$index;
       print "$substring is at: $pos\n";
       $index = $pos+$n;
    };
    And there are a number of other ways to code the solution to your (homework?) question.
    Don't expect the functions' documentation to tell you how to accomplish very specific tasks. It's up to you to apply the programming logic to solve your specific problem.

  11. #10
    lichy is offline Newbie
    Join Date
    Jun 2007
    Posts
    17
    Rep Power
    0
    once again, thanks for the help. much appreciated. =)

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts