Hi fellow Monks!
I would like to ask you to help me write the following code in a more "sophisticated" way.
What the code does is, given to strings of characters, like the following
$string1 = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII +IIIIIIIIIIMMMMMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIIMMMMMMMMMMM +MMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIMMMMMMMMMMMOOOOOOOOOOOOO +OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMIIIIIMMMMMMMM +MMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIMMMMMMMMMOOOOOOOOOO +OOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMIIIMMMMMMMMMOOOOOOOOOOOOOOOOOOOMMM +MMMMMMIIIIIIIIIIIIIIIIIIIIIIIIIIIMMMMMMMMOMMMMMMMMMMMMM"; $string2 = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII +IIIIIIIIIMMMMMMMMMMMMOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMMIIIIIMMMMMMMMMM +MMMOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMIIIMMMMMMMMMMMMMMOOOOOOOOOO +OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMIIIMMMMMMMMMM +MMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMIIIMMMMMMMMMMMMMMMOOOO +OOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMIIIMMMMMMMMMMMMOOOOOOOOOOOOOOOMMMM +MMMMMMIIIIIIMMMMMMMMMMMMOOOOOOOOOOOOOOOOOMMMMMMMMMMMMII";

the program should check the following:
1. Do both strings start with the same character? (in this case yes, they do)
2. Do both strings contain the same number of M substrings? (In this case yes, because both have 13).
3.Do each pair of the M substrings overlap for at least 5 positions (we would compare the 1st M substring of the first string to the 1st M substring of the seecond string and so on).
My code is the following, please bear with me because I am kind of new in Perl:
#N-terminal $Nterm_topo=substr($string2,0,1); $Nterm_pred=substr($string1,0,1); #TM number and orientation for $string2 $TM_topo=0; $all_tms=''; while($string2=~/M+/g) { $TM_topo++; $start_tm=$-[0]; $end_tm=$+[0]-1; $all_tms.=$start_tm."-".$end_tm."#"; } chop $all_tms; #TM number and orientation for $string1 $TM_pred=0; while($string1=~/(M+)/g) { $TM_pred++; } ################ Orientation ############################ $correct_TM_location=0; @split_tms_real=split(/#/, $all_tms); for ($i=0;$i<=$#split_tms_real;$i++) { $corresp_TM_real=$split_tms_real[$i]; if($corresp_TM_real=~/(\d+)\-(\d+)/) { $start_real=$1; $end_real=$2; $real_length=$end_real-$start_real+1; $substr_pred=substr($string1,$start_real, $real_length); $count_M=$substr_pred=~tr/M//; if($count_M>=5) {$correct_TM_location++;} } } if( $Nterm_topo eq $Nterm_pred $TM_topo==$TM_pred && $TM_pred==$corre +ct_TM_location) { $correct_topo++; }

In reply to Can you help me re-write this code? by Anonymous Monk

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.