Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not a perl expert but you can use $- and $+ to track index location for last regex match found.<br> Below is code built on top of your code that explains this. </p> <pre><code>use strict; use warnings; my $file="sample.txt"; open(DAT, $file) || die("Could not open file!"); open (OUTPUTFILE, '&gt;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 = &lt;DAT&gt;) { $someVar = 0; print "\nSequence $sequenceNums: $line\n"; print OUTPUTFILE "\nSequence $sequenceNums: $line\n"; if ($line=~ /$motif1/g) { &amp;printStuff($sequenceNums, "motif1", $motif1, "$-[0]-$+[0]"); $someVar = 1; } if ($line=~ /$motif2/g and $someVar == 1) { &amp;printStuff($sequenceNums, "motif2", $motif2, "$-[0]-$+[0]"); $someVar = 2; } if ($line=~ /$motif3/g and $someVar == 2) { &amp;printStuff($sequenceNums, "motif3", $motif4, "$-[0]-$+[0]"); $someVar = 3; } if ($line=~ /$motif4/g and $someVar == 3) { &amp;printStuff($sequenceNums, "motif4", $motif4, "$-[0]-$+[0]"); } else { $sequence .= $line; if ($someVar == 0) { &amp;printWrongStuff($sequenceNums, "motif1", $motif1); } elsif ($someVar == 1) { &amp;printWrongStuff($sequenceNums, "motif2", $motif2); } elsif ($someVar == 2) { &amp;printWrongStuff($sequenceNums, "motif3", $motif3); } elsif ($someVar == 3) { &amp;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); </code></pre> <p>Sample input:</p> <p>MLTSHQKKF<em>HDWFLSFKD</em>SNNYN<em>HD</em>S<em>K</em>QNHSIK<em>DD</em>IFNRFNHYIYNDLGIRTIA MLTSHQKKFSNNYNSKQNHSIKDIFNRFNHYIYNDLGIRTIA MLTSHQKKFSNNYNSK<em>HDWFLSFKD</em>QNHSIKDIFNRFNHYIYNDL</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload