Maybe not significantly more fun, but certainly more concise and probably faster: my $line = 'aabbaaabbb'; my $maxstr; while ($line =~ m{(.+).*\1}g) { if (length($1) > length($maxstr)) { $maxstr = $1; } } print "$maxstr\n"; -b "Edward M. Perry" wrote: > > Here's a fun bit of code to find the longest repeated substring. > Overlaps don't count. $line can contain any characters, not just > 'a' and 'b': > > #!/usr/bin/perl > > use strict; > my ($line) = 'aabbaaabbb'; > my ($try, $trylen, $str, $strlen); > > while ($line ne "") { > if ($line =~ m{^(.+)(.*)\1}) { > $try = $1; > $trylen = length($try); > > if ($trylen > $strlen) { > ($str, $strlen) = ($try, $trylen); > } > } > $line = substr($line, 1); > } > print "$str\n"; > > Problem is, it seems there should be a funner way to do it. > Not just a shorter way (I like named variables), but a fundamentally > funner way. > > ==== Want to unsubscribe from Fun With Perl? > ==== Well, if you insist... Send mail with body "unsubscribe" to > ==== fwp-request@technofile.org ==== Want to unsubscribe from Fun With Perl? ==== Well, if you insist... Send mail with body "unsubscribe" to ==== fwp-request@technofile.org