You aren't using the W switch like you're supposed to. The purpose of W is exactly to avoid what you have: a while loop. See this for the correct usage. Just replace $hmatch(table,text,1) with $hfind(table,text,1,W).