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

[FWP] Can it be funner?



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