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

时间:2018-06-21 00:29:57

标签: arrays string lua

尝试将其转换为字符串 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”,它就可以工作。我需要它不是字符串形式。

1 个答案:

答案 0 :(得分:2)

需要明确的是,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)