My question is, how could this be duplicated in Perl? Is there a technique for creating references to parts inside a scalar? I know there is a module for this, but it is simply a frontend to a c program. Can this be done strictly in Perl without using a pile of memory?
I took a shot at doing this, based on the DDJ code, but it is slow as bejeezus. Please Perl Monks, grant me your wisdom!!
#!/usr/bin/perl -w use strict; my $input; { local $/=undef; $input=<>; } my @arr=(0 .. (length($input)-2)); my @arr2=sort { makestr($a) cmp makestr($b) } @arr; my $maxlen=0; my $maxindex=0; for(my $i=0; $i<(length($input)-2); $i++) { my $temp=checklen($arr2[$i], $arr2[$i+1], \$input); if ($temp > $maxlen) { $maxlen=$temp; $maxindex=$i; } } print "maxlen:$maxlen index:$maxindex string:",substr($input,0,$maxlen +),"\n"; sub makestr{ return substr($input, $_[0], (length($input)-$_[0])); } sub checklen{ my $aa= $_[0]; my $bb=$_[1]; my $input=$_[2]; my $counter=0; my $len=length($$input)-1; while(($aa < $len)&& ($bb < $len)) { last unless (substr($$input, $aa++,1) eq substr($$input, $bb++,1)) +; $counter++; } return $counter; }
In reply to suffix arrays by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |