在LUA中将数组转换为字符串

5

尝试将 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
1个回答

4

明确一下,customLog是一个表格 - 也就是一个键值对的关联数组。以下是一种简单的方式来迭代所有的键值对,并将它们串联成一个字符串:

s = ""

t = {"a", "b", "c", 123, 456, 789} -- sample table
t.someKey = "some value" -- just an extra key value, to show that keys can be strings too

for k, v in pairs(t) do
    s = s .. k .. ":" .. v .. "\n" -- concatenate key/value pairs, with a newline in-between
end

print(s)

当然,如果一个键的值是另一个表{},那么您需要一些额外的逻辑来递归迭代这些嵌套的表。我会把这个留给您作为一个练习 :)
编辑1: 将表格作为字符串打印,显示变量值
s = ""
local ID = 123
local Numbers = 456
local Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} }

s = s .. "{"
for k, v in next, Log do
    s = s .. "{"

    for vk, vv in next, v do
        if next(v, vk) ~= nil then
            s = s .. vk .. " = " .. vv .. ", "
        else
            s = s .. vk .. " = " .. vv  
        end
    end

    if next(Log, k) ~= nil then
        s = s .. "}, "
    else
        s = s .. "}"
    end

end
s = s .. "}"

print(s)

编辑2: 将表格打印为字符串,显示变量名称
s = ""
local ID = 123
local Numbers = 456
local Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} }

s = s .. "{"
for k, v in next, Log do
    s = s .. "{"

    i = 1
    for vk, vv in next, v do
        name = debug.getlocal(1, i)
        if next(v, vk) ~= nil then
            s = s .. vk .. " = " .. name .. ", "
        else
            s = s .. vk .. " = " .. name
        end
        i = i + 1
    end

    if next(Log, k) ~= nil then
        s = s .. "}, "
    else
        s = s .. "}"
    end

end
s = s .. "}"

print(s)

接近但并不是我需要的。我需要直接将我的数组按原样放入另一个文件中。所以,我的数组示例是Log = {{Group = ID,Pos = Numbers},{Group = ID,Pos = Numbers}}。 - A Hamburgler
非常感谢您的帮助,它确实有效,但由于某种原因它无法传递变量。例如,它会说group = self而不是123。 - A Hamburgler
@AHamburgler 很高兴能帮忙。在你的代码中必须定义“self”。它不是Lua中的内置符号(使用冒号操作符的self概念是有的,但不是名称“self”)。如果您想发布更多的代码,我们可以尝试解决这个问题。 - brianolive
我确实使用了你的代码并进行了测试,结果显示group = self和Pos = event。它们在任何地方都没有被定义过。 - A Hamburgler
@AHamburgler,你使用的是编辑器1还是2?你在什么环境下进行编程 - 比如,在游戏脚本环境(Roblox、Corona等)中开发,还是只是使用自己的Lua文件?最后,如果你使用的是编辑器2,则可能在ID和Numbers之前定义了其他的局部变量。手动递增i计数器变量 - 这将查询堆栈上的其他局部变量。 - brianolive
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接