Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if a table is an array?

Tags:

lua

I'm developing a simple optimized JSON function. Lua uses tables to represent arrays but in JSON I need to recognize between them. The code below is used:

t={
    a="hi",
    b=100
}

function table2json(t,formatted)
if type(t)~="table" then return nil,"Parameter is not a table. It is: "..type(t)    end

local ret=""--return value
local lvl=0 --indentation level
local INDENT="  " --OPTION: the characters put in front of every line for indentation
function addToRet(str) if formatted then ret=ret..string.rep(INDENT,lvl)..str.."\n" else ret=ret..str end end

addToRet("{")
lvl=1
for k,v in pairs(t) do
    local typeof=type(v)
    if typeof=="string" then
        addToRet(k..":\""..v.."\"")
    elseif typeof=="number" then
        addToRet(k..":"..v)
    end
end
lvl=0
addToRet("}")

return ret
end

print(table2json(t,true))

As you can see in JSON reference an object is what is called a table in Lua and it's different from an array.

The question is how I can detect if a table is being used as an array?

  • One solution of course is to go through all pairs and see if they only have numerical consecutive keys but that's not fast enough.
  • Another solution is to put a flag in the table that says it is an array not an object.

Any simpler/smarter solution?

like image 710
AlexStack Avatar asked Sep 23 '11 08:09

AlexStack


People also ask

What is the difference between a table and an array?

Arrays collect values together in a strict order. Elements in the array are therefore accessed by their position within that sequence, called the element's index. Tables do not hold the values they collect in any order.

Is table an array Lua?

Introduction. Tables are the only data structure available in Lua that helps us create different types like arrays and dictionaries. Lua uses associative arrays and which can be indexed with not only numbers but also with strings except nil. Tables have no fixed size and can grow based on our need.


1 Answers

If you want fast, simple, non-intrusive solution that will work most of the times, then I'd say just check index 1 - if it exists, the table is an array. Sure, there's no guarantee, but in my experience, tables rarely have both numerical and other keys. Whether it's acceptable for you to mistake some objects for arrays and whether you expect this to happen often depend on your usage scenario - I guess it's not good for general JSON library.

Edit: For science, I went to see how Lua CJSON does things. It goes through all pairs and checks if all keys are integers while keeping the maximum key (the relevant function is lua_array_length). Then it decides whether to serialize the table as an array or object depending on how sparse the table is (the ratio is user controlled) i.e. a table with indices 1,2,5,10 will probably be serialized as an array while a table with indices 1,2,1000000 will go as an object. I guess this is actually quite good solution.

like image 139
sbk Avatar answered Oct 23 '22 12:10

sbk