尝试将 customLog2 = {}
转换为字符串格式,实际上就像这样:
Log = {{Group=ID,Pos=Numbers},{Group=ID,Pos=Numbers}}
。
我尝试过以下方法:
local Data = string.format( "LogBook = %s ", customLog2 )
但是由于CustomLog是一个数组而不是字符串或数字,所以我无法插入它。我正在尝试将数组转换为字符串,以便在VariableFile:write(Data)
中使用。如果有人能帮忙,那就太棒了,谢谢。
所以我希望我的输出看起来像这样:"local Data = string.format( "LogBook = %s ", customLog2 )"
这样我就可以使用 :write 然后在我新创建的文件中它应该看起来像这样 Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} }
所以这个函数工作得很好,除了一个问题。
function TableSerialization(t, i)
local text = "{\n"
local tab = ""
for n = 1, i + 1 do --controls the indent for the current text line
tab = tab .. "\t"
end
for k,v in pairs(t) do
if type(k) == "string" then
text = text .. tab .. "['" .. k .. "'] = "
else
text = text .. tab .. "[" .. k .. "] = "
end
if type(v) == "string" then
text = text .. "'" .. v .. "',\n"
elseif type(v) == "number" then
text = text .. v .. ",\n"
elseif type(v) == "table" then
text = text .. TableSerialization(v, i + 1)
elseif type(v) == "boolean" then
if v == true then
text = text .. "true,\n"
else
text = text .. "false,\n"
end
elseif type(v) == "function" then
text = text .. v .. ",\n"
elseif v == nil then
text = text .. "nil,\n"
end
end
tab = ""
for n = 1, i do --indent for closing bracket is one less then previous text line
tab = tab .. "\t"
end
if i == 0 then
text = text .. tab .. "}\n" --the last bracket should not be followed by an comma
else
text = text .. tab .. "},\n" --all brackets with indent higher than 0 are followed by a comma
end
return text
end
我的输入数组看起来像这样 Log = { Group = WestAPC },但这样不起作用,因为WestAPC不是一个字符串。但是如果WestAPC看起来像这样"WestAPC",那么它就能工作了。我需要它不以字符串形式存在。
table.concat
。 - lhf{ Group = WestAPC }
和{ Group = "WestAPC" }
都构建了一个只有一个字段的表,该字段的键名为字符串 "Group"。值从相应的表达式中设置。在第二个表达式中,表达式是一个字面字符串。在第一个表达式中,表达式是一个变量。变量的值可能是nil
、字符串或任何其他类型的值。在表中,如果字段值被设置为nil
,则键值对将从表中删除。那么,你的目标是什么?也许问题出在你还没有展示的代码上。 - Tom Blodget