ericleasemorgan,
To be honest, changing %words to a more appropriate data structure would probably be best. This way you could walk the tree. Unfortunately, without knowing how %words is used by the rest of the program or how it is maintained, that is a blind recommendation.