I note that the code doesn't actually run because line 39 is not valid Perl. I also note that there is no effect other than caching the correctness of the string in $correct_topo, so there are probably some real wins to be had by broader restructuring (using subroutines and modules, early exits, Loop Control, structuring data, ...).

Why are you caching a bunch of temporary variables rather than actually performing each test in sequence? It would seem much more logical to check if your substr expressions are equal, rather than creating two variables with cryptic names. You also do a lot with regular expressions that is probably excessive.

The most basic recommendation is to use strict and warnings -- see Use strict warnings and diagnostics or die for a discussion of why.

You could also get a lot of traction from the index command if you actually know what letter you are looking for.

#!/usr/bin/perl use strict; use warnings; my $string1 = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII +IIIIIIIIIIIIIMMMMMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIIMMMMMMMM +MMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIMMMMMMMMMMMOOOOOOOOOO +OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMIIIIIMMMMM +MMMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIMMMMMMMMMOOOOOOO +OOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMIIIMMMMMMMMMOOOOOOOOOOOOOOOOOOO +MMMMMMMMMIIIIIIIIIIIIIIIIIIIIIIIIIIIMMMMMMMMOMMMMMMMMMMMMM"; my $string2 = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII +IIIIIIIIIIIIMMMMMMMMMMMMOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMMIIIIIMMMMMMM +MMMMMMOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMIIIMMMMMMMMMMMMMMOOOOOOO +OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMMIIIMMMMMMM +MMMMMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMIIIMMMMMMMMMMMMMMMO +OOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMMMIIIMMMMMMMMMMMMOOOOOOOOOOOOOOOM +MMMMMMMMMIIIIIIMMMMMMMMMMMMOOOOOOOOOOOOOOOOOMMMMMMMMMMMMII"; my $correct_topo = 1; # Test if they start with the same letter if (substr($string1,0,1) ne substr($string2,0,1)) { undef $correct_topo; } # Test if M counts match up my $count1; $count1++ while $string1 =~ /M+/g; my $count2; $count2++ while $string2 =~ /M+/g; if ($count1 != $count2) { undef $correct_topo; } # Test if M's line up my $start = index($string1, 'M'); $start = index($string2, 'M', $start); # In case it starts later while ($start != -1) { # While I haven't missed anything $count1--; # See of the counts line up, in case we skip collisions if ( substr($string1,$start,5) ne 'MMMMM' or substr($string2,$start,5) ne 'MMMMM') { undef $correct_topo; last; } substr($string1,$start) =~ /[^M]/ or last; # Ran out of string $start = index $string1, 'M', $-[0]+$start; # Start of next set of + M's $start = index $string2, 'M', $start if $start != -1; # But this c +hunk might start later } undef $correct_topo if $count1 != 0;

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: Can you help me re-write this code? by kennethk
in thread 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.