I have a nice little Lua table parser which prints out pretty looking lua code and I love it...it works beautifully. There is a slight snag...if I go to print a table or array that has any integer keys it loops through it using pairs
(which doesn't mess the code up ironically), but I would rather it use ipairs
if possible. So I want to know is it possible to check a table (without physically looking at it) if it can use ipairs to loop through it first else use pairs. Then is there a way to start looping at 0 instead of Lua's default 1?
Lua Table Parser (Base code found on google, changed it to make it print more array friendly)...
function TableParser(name, object, tabs)
local function serializeKeyForTable(k)
if type(k)=="number" then
return ""
end
if string.find(k,"[^A-z_-]") then
return k
end
return k
end
local function serializeKey(k)
if type(k)=="number" then
if k == 0 then
return "\t[" .. k .."] = "
else
return "\t"
end
end
if string.find(k,"[^A-z_-]") then
return "\t" .. k .. " = "
end
return "\t" .. k .. " = "
end
if not tabs then tabs = "" end
local function serialize(name, object, tabs) -- = {
local output = tabs .. (name ~= "" and name .. " = " or "") .. "{" .. "\n"
for k,v in pairs(object) do
if type(v) == "number" then
output = output .. tabs .. serializeKey(k) .. v
elseif type(v) == "string" then
output = output .. tabs .. serializeKey(k) .. string.format("%q",v)
elseif type(v) == "table" then
output = output .. serialize(serializeKeyForTable(k), v, tabs.."\t")
elseif type(v) == "boolean" then
output = output .. tabs .. serializeKey(k) .. tostring(v)
else
output = output .. tabs .. serializeKey(k) .. "\"" .. tostring(v) .. "\""
end
if next(object,k) then
output = output .. ",\n"
end
end
return output .. "\n" .. tabs .. "}"
end
return serialize(name, object, tabs)
end
if string.find(k, ..
seems completely pointless. You're returning the same result in both cases regardless of the conditional outcome.