【发布时间】:2018-11-30 05:02:56
【问题描述】:
试图将其转换为字符串
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。 -
{ Group = WestAPC }与{ Group = "WestAPC" }都构造了一个表,其中包含具有相同键的单个字段,即字符串“Group”。该值是根据相应的表达式设置的。在第二种情况下,表达式是一个文字字符串。首先,表达式是一个变量。变量的值可能是nil、字符串或任何其他类型的值。在表中,如果字段值设置为nil,则从表中删除键值对。那么,你在这里的目标是什么?也许问题出在您尚未显示的代码中。