Returning the index of a value in a Lua table

lualua-table

I have this table in lua:

local values={"a", "b", "c"}

is there a way to return the index of the table if a variable equals one the table entries?
say

local onevalue = "a"

how can I get the index of "a" or onevalue in the table without iterating all values?

Best Answer

There is no way to do that without iterating.

If you find yourself needing to do this frequently, consider building an inverse index:

local index={}
for k,v in pairs(values) do
   index[v]=k
end
return index["a"]
Related Topic