### Comment on

 Need Help??

UPDATE: WARNING: the following code does not work in all circumstances. Sorry!

Here is a variant of tobyink's solution that uses index to look ahead when the current candidate string repeats and then enlengthens (is that an English word?) it accordingly.

```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 c
+andidate
return \$input if \$i < 0;            # if not found return full
+ string => no repetition
\$possible = substr \$input, 0, \$i;   # this is the minimum leng
+th candidate
return \$possible if \$input eq substr(\$possible x (1 + int(\$len
+gth / \$i)), 0, \$length); # success
}
}

UPDATE: Eily's solution below Re^3: Finding repeat sequences. can be used to avoid the construction of the repeated string (as it is the same just with offset). Therefore, this works even better:

```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(\$i
+nput, 0, \$length - \$i);
}
}

In reply to Re^3: Finding repeat sequences. by hdb
in thread Finding repeat sequences. by BrowserUk

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, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
 [karlgoethebier]: Lady_Aleena: Try it! [Lady_Aleena]: marioroy, it has 3 subroutines which can be fed data to generate random things. See here. [Lady_Aleena]: marioroy, it is used in 24 other modules I wrote. [Lady_Aleena]: GUH! I wrote a script better than I thought! 8)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-05-29 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.