You can get away with using the Lvalues from substr in the array as suggested by Zaxo above, but you have to jump through a few hoops to prevent the problem I alluded to.

Basically, there seems to be a single slot allocated for all the Lvalues generated by any given statement that includes a substr. That's a clumsy way to put it, but I can't think of a better way to say it.

To work around the limitation, it is necessary to use a 'different' substr each time, hence the need for the eval in this code. The downside is that the eval markly reduces the efficiency. The algorithm works fine though and this implementation shoudl be a bit quicker than yours.

#! perl -slw use strict; sub ATOa{ map{ eval '\substr( $_[0], $_ )' } 0 .. length( $_[0] ); } my @Aoa = sort{ $$a cmp $$b } ATOa $ARGV[0]; #print $$_ for @Aoa; my $max = ''; for my $i ( 0 .. ($#Aoa-1) ) { my $p = 0; ++$p while substr( ${$Aoa[ $i ]}, 0, $p ) eq substr( ${$Aoa[ $i+1 ]}, 0, $p ); $max = substr( ${$Aoa[ $i ]}, 0, $p-1 ) if length $max < $p; } print "The longest common substring in\n'$ARGV[0]'\n is '$max'"; __DATA__ P:\test>test "the infernal invention was the source or eternal facinat +ion for the qualified professional and enthusiastic amateur alike" The longest common substring in 'the infernal invention was the source of eternal facination for the q +ualified professional and enthusiastic amateur alike' is 'ernal ' P:\test>test "Four and twenty virgins came down from Inverness. And wh +en they went back again there were four and twenty less" The longest common substring in 'Four and twenty virgins came down from Inverness. And when they went +back again there were four and twenty less' is 'our and twenty '

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


In reply to Re: suffix arrays by BrowserUk
in thread suffix arrays by Anonymous Monk

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