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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Longest String Matching
by kcott (Archbishop) on Apr 08, 2013 at 12:14 UTC | |
Re: Longest String Matching
by space_monk (Chaplain) on Apr 08, 2013 at 11:56 UTC | |
by hdb (Monsignor) on Apr 08, 2013 at 12:02 UTC | |
by space_monk (Chaplain) on Apr 08, 2013 at 12:08 UTC | |
by hdb (Monsignor) on Apr 08, 2013 at 12:17 UTC | |
Re: Longest String Matching
by choroba (Cardinal) on Apr 08, 2013 at 11:57 UTC | |
Re: Longest String Matching
by hdb (Monsignor) on Apr 08, 2013 at 11:49 UTC | |
Re: Longest String Matching
by Rahul6990 (Beadle) on Apr 08, 2013 at 12:03 UTC | |
Re: Longest String Matching
by AnomalousMonk (Archbishop) on Apr 09, 2013 at 01:40 UTC | |
by space_monk (Chaplain) on Apr 09, 2013 at 14:27 UTC | |
Re: Longest String Matching
by grizzley (Chaplain) on Apr 08, 2013 at 14:19 UTC | |
Re: Longest String Matching
by pemungkah (Priest) on Apr 08, 2013 at 20:35 UTC |
Back to
Seekers of Perl Wisdom