[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Can it be funner?



Ok, here we go. Sorry for spamming the list. I'll test more thoroughly
next time! :-)

my $line = 'aabbaaabbb';
my $maxstr;
while ($line =~ m{(?=(.+).*\1)}g) {
  print "$1\n";
  if (length($1) > length($maxstr)) {
    $maxstr = $1;
  }
}
print "$maxstr\n";


"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