You can also organize this with metatables in a completely transparent way:
mt={__newindex=function(t,k,v) if type(k)~='string' then error'this table only takes string keys' else rawset(t,k:lower(),v) end end, __index=function(t,k) if type(k)~='string' then error'this table only takes string keys' else return rawget(t,k:lower()) end end} keywords=setmetatable({},mt) for idx,word in pairs{"word","test","blah","here","code","woot"} do keywords[word]=idx; end for idx,word in ipairs{"Foo","HERE",'WooT'} do local res=keywords[word] if res then print(("%s at index %d in given array matches index %d in keywords"):format(word,idx,keywords[word] or 0)) else print(word.." not found in keywords") end end
Thus, the table can be indexed in any case. If you add new words to it, it will also automatically reduce them. You can even customize it to fit with templates or whatever you want.
source share