Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I am trying to write a script to match the longest string at the end of a word. I am trying to do this by splitting each word into an array, so that each letter becomes an array-element. I then have a hash(%lexicon) with a couple of words stored. If I have the words 'own' and 'known' in my hash, and I try to find the longest string at the end of the word 'unknown', the result should be 'known'. However the best I can do is get back 'own'. unknown = unkn + own, which is not what I want.

my $end; my $mover; my $right; foreach my $q (keys %wordlist) { push (@array, split(//, $q)); $end = $#array; $mover = $end-2; &rightside(); print "$right\n"; @array = (); } sub rightside { if (exists $lexicon{join q(), @array[$mover..$end]}) { $right = join q(), @array[$mover..$end]; } else { $mover--; &rightside(); } }

I tried to replace the subroutine with this

sub rightside { for (my $x = $#array-2; $x == 0; $x--) { if (exists $lexicon{join q(), @array[$x..$#array]}) { $right = join q(), @array[$x..$#array]; } } }

But it doesn't work either. Any help would be appreciated