我有一个漂亮的小Lua表解析器打印出漂亮的lua代码,我喜欢它…它的工作非常精彩.有一个轻微的障碍……如果我去打印一个有任何整数键的表或数组,它会使用对来循环它(这不会讽刺代码
Lua Table Parser(在谷歌上找到的基本代码,更改它以使其打印更多阵列友好)…
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
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.
不要检查,只是做!首先使用ipairs并跟踪ipairs迭代器返回的最大键.然后使用对再次迭代并忽略1和ipairs中最大键之间的所有整数键.
如果你真的想检查ipairs是否会做某事,那么看一下表中的索引1(rawget(object,1)〜= nil).如果没有迭代表,检查ipairs是否将覆盖表中的所有元素是不可能的.
Then is there a way to start looping at 0 instead of Lua’s default 1?
ipairs(t)返回三个值:迭代器函数,表t作为状态变量,初始索引值为0.如果使用-1作为初始索引值,则ipairs将从0开始迭代(迭代器函数总是递增在使用索引值之前一个):
t = { 1, 2, 3, [ 0 ] = 0 }
for i,v in ipairs( t ), t, -1 do -- only use first value returned by ipairs
print( i, v )
end
但是,请注意Lua 5.2已添加对新元方法__ipairs的支持,它允许您返回用于ipairs迭代的自定义迭代器三元组,并且在这种情况下返回的迭代器函数可能需要不同的状态和初始索引值.
编辑:
要将建议合并到for k之前的代码插入中,v成对(对象)执行循环:
local largest = 0
for k,v in ipairs(object) do
largest = k
local t = type(v)
if t == "table" then
output = output .. tabs .. "\t" .. serialize( "", v, tabs.."\t" )
elseif t == "string" then
output = output .. tabs .. "\t" .. string.format("%q", v)
else
output = output .. tabs .. "\t" .. tostring(v)
end
output = output .. ",\n"
end
并在循环内添加一个额外的if语句来检查数组键:
for k,v in pairs(object) do
if type(k) ~= "number" or k < 1 or k > largest or math.floor(k) ~= k then
-- if type(v) == "number" then
-- ...
end
end
如果将此修改后的TableParser函数应用于下表:
local t = {
1, 2, 3,
value = "x",
tab = {
"a", "b", field = "y"
}
}
print( TableParser( "", t ) )
输出是:
{
1,
2,
3,
tab = {
"a",
"b",
field = "y"
},
value = "x"
}
但是正确地进行表序列化是很棘手的.例如.您的实现不会将循环或表作为键处理.有些实现,请参见Lua Wiki.
