http://www.perlmonks.org?node_id=1027476


in reply to Longest String Matching

I'm open to better ideas.... :-)
while (length($word) && !exists $lookup{$word}) { substr($word, 0, 1) = ""; # remove 1st char } # $word will be empty or the matched string

Update: This probably works well if the lexicon contains a lot of words as the execution time of this is proportional only to the length of the word. hdb has contributed an improvement to reduced the complexity a little, using the common trick of guaranteeing a match for zero length. There is a regex solution below this which is interesting too...

A Monk aims to give answers to those who have none, and to learn from those who know more.