Lua - 将表转换为逗号分隔列表

时间:2022-09-02 00:21:08

I need to convert a table into a comma separated list in order to save it to a text file. Is there a built in method for doing this in Lua?

我需要将表转换为逗号分隔列表,以便将其保存到文本文件中。是否有内置的方法在Lua中执行此操作?

4 个解决方案

#1


2  

There isn't a built in function, but there are examples onthe web.

没有内置功能,但网上有例子。

This is a decent one actually.

实际上这是一个体面的。

#2


11  

If your table is an array, you can use table.concat to print CSVs:

如果表是数组,则可以使用table.concat打印CSV:

t={10,20,30}
print(table.concat(t,","))

outputs 10,20,30.

输出10,20,30。

#3


0  

There is not a built in way, but there are a number of options that are relatively easy if you want to build it yourself. Here are some links that can help you figure out how you want to put it together:

没有内置方式,但如果您想自己构建它,有许多选项相对容易。以下是一些链接,可以帮助您弄清楚如何将它们组合在一起:

http://www.lua.org/pil/12.1.html
http://lua-users.org/wiki/TableSerialization

http://www.lua.org/pil/12.1.html http://lua-users.org/wiki/TableSerialization

#4


0  

No, there is not a "built in" function for this. But it's not hard to do it yourself. I keep a script around for recursively writing Lua tables directly to files as Lua scripts, which can then be loaded and executed like Lua scripts.

不,没有“内置”功能。但是自己做并不难。我保留一个脚本,以递归方式将Lua表作为Lua脚本直接写入文件,然后可以像Lua脚本一样加载和执行。

--This file exports a function, WriteTable, that writes a given table out to a given file handle.

local writeKey = {};

function writeKey.string(hFile, value, iRecursion)
    WriteFormatted(hFile, "[\"%s\"]", value);
end

function writeKey.number(hFile, value, iRecursion)
    WriteFormatted(hFile, "[%i]", value);
end

local writeValue = {};

function writeValue.string(hFile, value, iRecursion)
    WriteFormatted(hFile, "[==[%s]==]", value);
end

function writeValue.number(hFile, value, iRecursion)
    WriteFormatted(hFile, "%i", value);
end

function writeValue.boolean(hFile, value, iRecursion)
    if(value) then hFile:write("true"); else hFile:write("false"); end;
end

function writeValue.table(hFile, value, iRecursion)
    WriteTable(hFile, value, iRecursion)
end

local function WriteFormatted(hFile, strFormat, ...)
    hFile:write(string.format(strFormat, ...));
end

local function WriteForm(hFile, strFormat, ...)
    hFile:write(string.format(strFormat, ...));
end

local function WriteTabs(hFile, iRecursion)
    for iCount = 1, iRecursion, 1 do
        hFile:write("\t");
    end
end

function WriteTable(hFile, outTable, iRecursion)
    if(iRecursion == nil) then iRecursion = 1; end

    hFile:write("{\n");

    local bHasArray = false;
    local arraySize = 0;

    if(#outTable > 0) then bHasArray = true; arraySize = #outTable; end;

    for key, value in pairs(outTable) do
        if(writeKey[type(key)] == nil) then print("Malformed table key."); return; end
        if(writeValue[type(value)] == nil) then
            print( string.format("Bad value in table: key: '%s' value type '%s'.", key, type(value)));
            return;
        end

        --If the key is not an array index, process it.
        if((not bHasArray) or
                (type(key) ~= "number") or
                not((1 <= key) and (key <= arraySize))) then
            WriteTabs(hFile, iRecursion);
            writeKey[type(key)](hFile, key, iRecursion + 1);
            hFile:write(" = ");
            writeValue[type(value)](hFile, value, iRecursion + 1);

            hFile:write(",\n");
        end
    end

    if(bHasArray) then
        for i, value in ipairs(outTable) do
            WriteTabs(hFile, iRecursion);
            writeValue[type(value)](hFile, value, iRecursion + 1);
            hFile:write(",\n");
        end
    end

    WriteTabs(hFile, iRecursion - 1);
    hFile:write("}");
end

#1


2  

There isn't a built in function, but there are examples onthe web.

没有内置功能,但网上有例子。

This is a decent one actually.

实际上这是一个体面的。

#2


11  

If your table is an array, you can use table.concat to print CSVs:

如果表是数组,则可以使用table.concat打印CSV:

t={10,20,30}
print(table.concat(t,","))

outputs 10,20,30.

输出10,20,30。

#3


0  

There is not a built in way, but there are a number of options that are relatively easy if you want to build it yourself. Here are some links that can help you figure out how you want to put it together:

没有内置方式,但如果您想自己构建它,有许多选项相对容易。以下是一些链接,可以帮助您弄清楚如何将它们组合在一起:

http://www.lua.org/pil/12.1.html
http://lua-users.org/wiki/TableSerialization

http://www.lua.org/pil/12.1.html http://lua-users.org/wiki/TableSerialization

#4


0  

No, there is not a "built in" function for this. But it's not hard to do it yourself. I keep a script around for recursively writing Lua tables directly to files as Lua scripts, which can then be loaded and executed like Lua scripts.

不,没有“内置”功能。但是自己做并不难。我保留一个脚本,以递归方式将Lua表作为Lua脚本直接写入文件,然后可以像Lua脚本一样加载和执行。

--This file exports a function, WriteTable, that writes a given table out to a given file handle.

local writeKey = {};

function writeKey.string(hFile, value, iRecursion)
    WriteFormatted(hFile, "[\"%s\"]", value);
end

function writeKey.number(hFile, value, iRecursion)
    WriteFormatted(hFile, "[%i]", value);
end

local writeValue = {};

function writeValue.string(hFile, value, iRecursion)
    WriteFormatted(hFile, "[==[%s]==]", value);
end

function writeValue.number(hFile, value, iRecursion)
    WriteFormatted(hFile, "%i", value);
end

function writeValue.boolean(hFile, value, iRecursion)
    if(value) then hFile:write("true"); else hFile:write("false"); end;
end

function writeValue.table(hFile, value, iRecursion)
    WriteTable(hFile, value, iRecursion)
end

local function WriteFormatted(hFile, strFormat, ...)
    hFile:write(string.format(strFormat, ...));
end

local function WriteForm(hFile, strFormat, ...)
    hFile:write(string.format(strFormat, ...));
end

local function WriteTabs(hFile, iRecursion)
    for iCount = 1, iRecursion, 1 do
        hFile:write("\t");
    end
end

function WriteTable(hFile, outTable, iRecursion)
    if(iRecursion == nil) then iRecursion = 1; end

    hFile:write("{\n");

    local bHasArray = false;
    local arraySize = 0;

    if(#outTable > 0) then bHasArray = true; arraySize = #outTable; end;

    for key, value in pairs(outTable) do
        if(writeKey[type(key)] == nil) then print("Malformed table key."); return; end
        if(writeValue[type(value)] == nil) then
            print( string.format("Bad value in table: key: '%s' value type '%s'.", key, type(value)));
            return;
        end

        --If the key is not an array index, process it.
        if((not bHasArray) or
                (type(key) ~= "number") or
                not((1 <= key) and (key <= arraySize))) then
            WriteTabs(hFile, iRecursion);
            writeKey[type(key)](hFile, key, iRecursion + 1);
            hFile:write(" = ");
            writeValue[type(value)](hFile, value, iRecursion + 1);

            hFile:write(",\n");
        end
    end

    if(bHasArray) then
        for i, value in ipairs(outTable) do
            WriteTabs(hFile, iRecursion);
            writeValue[type(value)](hFile, value, iRecursion + 1);
            hFile:write(",\n");
        end
    end

    WriteTabs(hFile, iRecursion - 1);
    hFile:write("}");
end