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

发布于 2024-11-18 18:10:20 字数 56 浏览 2 评论 0原文

我需要将表转换为逗号分隔的列表,以便将其保存到文本文件中。 Lua 中有内置方法可以做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人的歌 2024-11-25 18:10:20

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

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

输出 10,20,30

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

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

outputs 10,20,30.

猫腻 2024-11-25 18:10:20

没有内置函数,但网上有示例。

这实际上是一个不错的。

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

This is a decent one actually.

幸福丶如此 2024-11-25 18:10:20

不,没有为此的“内置”功能。但自己做并不难。我保留了一个脚本,用于将 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

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.

--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
明媚如初 2024-11-25 18:10:20

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

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

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

栀子花开つ 2024-11-25 18:10:20

是的,有一个内置方法,并且它已经存在很长时间了。

-- table.concat
local line = { "Fred", 20, 4.000 }
print(table.concat(line,","))

输出:Fred, 20, 4.000

您可以使用此函数将表格转换为字符串,只需选择“,”作为分隔符即可。
您还可以添加一个在串联期间运行的函数,并检测您编写了多少个属性,然后添加一个新行 - 如果需要,您可以制作一个非常复杂的转换器。

我的建议是打破逗号分隔表的每一“行”,并用“,”连接每个“行”,然后将其写出来。这样您就可以确保可以处理大量行,并且每行的格式都正确。

注意事项:

  • 您必须处理带有逗号、引号等的字符串。
  • 该方法主要针对有序表(列表或数组)。它们必须被索引。
  • 如果您需要对表中的值进行处理,请先执行此操作。然后连接。

连接参考:
http://www.lua.org/manual/5.1/ Manual.html#pdf-table.concat

Yes, there is a builtin method, and its been around for a very long time.

-- table.concat
local line = { "Fred", 20, 4.000 }
print(table.concat(line,","))

Output: Fred, 20, 4.000

You can convert a table to a string using this function, and simply choose a "," for a separator.
You can also add a function that runs during concatenation and detects how many properties you have written, then add a new line - you can make a very complex converter if you need.

My recommendation is to break each "line" of comma separated tables and concat each one with "," then write it out. This way you can be sure that you can handle large numbers of lines, and that each line is correctly formatted.

Caveats:

  • You will have to handle strings with commas, quotes and so forth.
  • This method is mainly for ordered tables (lists or arrays). They must be indexed.
  • If you need to do processing on your values in the table, do it first. Then concat.

Reference for concat:
http://www.lua.org/manual/5.1/manual.html#pdf-table.concat

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文