in reply to Can you help me re-write this code?

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.