Let's say I have this code:
local testtbl = {"foo", "bar", "baz"}
How do I get the index of an element? For example:
print(indexOf(testtbl, "bar")) -- 2
Let's say I have this code:
local testtbl = {"foo", "bar", "baz"}
How do I get the index of an element? For example:
print(indexOf(testtbl, "bar")) -- 2
You need to iterate over the table with ipairs, like so:
function indexOf(tbl, value)
for i, v in ipairs(tbl) do
if v == value then
return i
end
end
end
tbl is a sequence (has only consecutive integer keys starting at 1). I'd probably use pairs for a more general solution which will still work for sequences.