Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 196023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:43:55+00:00 2026-05-11T16:43:55+00:00

The following is the script for finding consecutive substrings in strings. use strict; use

  • 0

The following is the script for finding consecutive substrings in strings.

use strict;
use warnings;

my $file="Sample.txt";
open(DAT, $file) || die("Could not open file!");

#worry about these later
#my $regexp1 = "motif1";
#my $regexp2 = "motif2";
#my $regexp3 = "motif3";
#my $regexp4 = "motif4";

my $sequence;

while (my $line = <DAT>) {
    if ($line=~ /(HDWFLSFKD)/g){
        {
        print "its found index location: ",
        pos($line), "-",  pos($line)+length($1), "\n";        
        }
        if ($line=~ /(HD)/g){
                print "motif found and its locations is: \n";
                pos($line), "-", pos($line)+length($1), "\n\n";
                }
                if ($line=~ /(K)/g){
                        print "motif found and its location is: \n";
                        pos($line), "-",pos($line)+length($1), "\n\n";
                        }
                        if ($line=~ /(DD)/g){
                                print "motif found and its location is: \n";
                                pos($line), "-", pos($line)+length($1), "\n\n";
                                }
}else {
        $sequence .= $line;
        print "came in else\n";
    }
}

It matches substring1 with string and prints out position where substring1 matched. The problem lies in finding the rest of the substrings. For substrings2 it starts again from the beginning of the string (instead of starting from the position where substring1 was found). The problem is that every time it calculates position it starts from the beginning of string instead of starting from the position of the previously found substring. Since substrings are consecutive substring1, substring2, substring3, substring4, their positions have to occur after the previous respectively.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:43:55+00:00Added an answer on May 11, 2026 at 4:43 pm

    I’m not a perl expert but you can use $- and $+ to track index location for last regex match found.
    Below is code built on top of your code that explains this.

    use strict;
    use warnings;
    
    
    my $file="sample.txt";
    open(DAT, $file) || die("Could not open file!");
    
    open (OUTPUTFILE, '>data.txt');
    
    my $sequence;
    my $someVar = 0;
    my $sequenceNums = 1;
    
    my $motif1 = "(HDWFLSFKD)";
    my $motif2 = "(HD)";
    my $motif3 = "(K)";
    my $motif4 = "(DD)";
    
    while (my $line = <DAT>) 
    {
        $someVar = 0;
        print "\nSequence $sequenceNums: $line\n";
        print OUTPUTFILE "\nSequence $sequenceNums: $line\n";
            if ($line=~ /$motif1/g)
            {
                    &printStuff($sequenceNums, "motif1", $motif1, "$-[0]-$+[0]");
                    $someVar = 1;
            }
    
    
            if ($line=~ /$motif2/g and $someVar == 1)
            {
                    &printStuff($sequenceNums, "motif2", $motif2, "$-[0]-$+[0]");
                    $someVar = 2;
            }
    
            if ($line=~ /$motif3/g and $someVar == 2)
            {
                    &printStuff($sequenceNums, "motif3", $motif4, "$-[0]-$+[0]");
                    $someVar = 3;
            }
    
            if ($line=~ /$motif4/g and $someVar == 3)
            {
                    &printStuff($sequenceNums, "motif4", $motif4, "$-[0]-$+[0]");
            }
    
            else 
            {
                $sequence .= $line;
    
                if ($someVar == 0)
                {
                    &printWrongStuff($sequenceNums, "motif1", $motif1);
                }
                elsif ($someVar == 1)
                {
                &printWrongStuff($sequenceNums, "motif2", $motif2);
                }
                elsif ($someVar == 2)
                {
                &printWrongStuff($sequenceNums, "motif3", $motif3);
                }
                elsif ($someVar == 3)
                {
                &printWrongStuff($sequenceNums, "motif4", $motif4);
                }
            }
            $sequenceNums++;
    }
    
    sub printStuff
    {
                print "Sequence: $_[0] $_[1]: $_[2] index location: $_[3] \n";
                print OUTPUTFILE "Sequence: $_[0]  $_[1]: $_[2] index location: $_[3]\n";
    }
    
    sub printWrongStuff
    {
                print "Sequence: $_[0] $_[1]: $_[2] was not found\n";
                print OUTPUTFILE "Sequence: $_[0] $_[1]: $_[2] was not found\n";    
    
    }
    
    close (OUTPUTFILE);
    close (DAT);
    

    Sample input:

    MLTSHQKKFHDWFLSFKDSNNYNHDSKQNHSIKDDIFNRFNHYIYNDLGIRTIA
    MLTSHQKKFSNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA
    MLTSHQKKFSNNYNSKHDWFLSFKDQNHSIKDIFNRFNHYIYNDL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this is the VB code to do so... myListBox.SelectionMode =… May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer Check out IsWow64Process. May 11, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer UPDATE t_transaction tu JOIN ( SELECT code, MAX(flag) AS flag… May 11, 2026 at 11:52 pm

Related Questions

The following script is for finding one motif in protein sequence. use strict; use
A lot of questions have been asked about gem support in Shoes, but none
Is there a simple way to run a Python script on Windows/Linux/OS X? On
I need assistance finding a delivery method that best fulfills the following requirements: We

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.