sub find_substring { my $input = shift; my $length = length $input; my $i = 0; my $possible; while( 1 ) { $possible = substr $input, 0, $i+1; # increase length by 1 $i = index $input, $possible, $i+1; # find next occurence of candidate return $input if $i < 0; # if not found return full string => no repetition $possible = substr $input, 0, $i; # this is the minimum length candidate return $possible if $input eq substr($possible x (1 + int($length / $i)), 0, $length); # success } } #### sub find_substring { my $input = shift; my $length = length $input; my $i = 0; while( 1 ) { $i = index( $input, substr( $input, 0, $i+1 ), $i+1); return $input if $i < 0; return substr( $input, 0, $i) if substr( $input, $i ) eq substr($input, 0, $length - $i); } }