27

As the title says, what function or check can I do to find out if a lua element is a table or not?

local elem = {['1'] = test, ['2'] = testtwo}
if (elem is table?) // <== should return true
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
unwise guy
  • 1,048
  • 8
  • 18
  • 27

4 Answers4

40
print(type(elem)) -->table

the type function in Lua returns what datatype it's first parameter is (string)

Nathan
  • 430
  • 5
  • 7
33

In the context of the original question,

local elem = {['1'] = test, ['2'] = testtwo}
if (type(elem) == "table") then
  -- do stuff
else
  -- do other stuff instead
end

Hope this helps.

Hugh
  • 341
  • 3
  • 3
10

You may find this helps readability:

local function istable(t) return type(t) == 'table' end
mlepage
  • 2,372
  • 2
  • 19
  • 15
4

Use type():

local elem = {1,2,3}
print(type(elem) == "table")
-- true
furq
  • 5,648
  • 3
  • 16
  • 21