Is this your case?
local shapes = { }
local function callback(shape_one)
-- ???
end
local the_shape = { is_circle = false }
shapes[4] = the_shape
assert(callback(the_shape) == 4)
Both shapes[4] and the_shape contain a reference to value, but in Lua there is no other connection between these two variables. So, you can not say "index of a variable in a table", you should rather say "index of a value in a table, that matches the value in a variable". What exactly "matches" is depends on your case. In this case you most likely looking for reference equality.
Note that in Lua all table values are unique, so the_shape ~= { is_circle = false } (that is, new table with identical content), but the_shape == shapes[4] (both refer to the same value). You can compare tables by value if needed, but that's a separate topic.
So, if you really want to find an index of the value in table, you have to search it there manually. Either do a linear search:
local function callback(shape_one)
for k, v in pairs(shapes) do
if v == shape_one then
return k
end
end
return nil, "shape not found" -- or maybe call error() here
end
...Or cache all shapes:
local function tflip(t)
local r = { }
for k, v in pairs(t) do
r[v] = k -- overrides duplicate values if any
end
return r
end
local shape_index = tflip(shapes)
local function callback(shape_one)
return shape_index[shape_one] -- will return nil if not found
end
Note that shape_index would, of course, prevent garbage collection of its contents. Assuming its lifetime is the same as lifetime of shapes table and it is kept in sync with it, this is not a problem. If this is not your case, you may configure the shapes table to be weak-keyed. (Tell me if you want me to expand this point.)
BTW, you may keep shape_index up to date automatically with some metatable magic. Tell me if you want this explained as well, I will update the answer.