开发者

Lua - convert a table into a comma separated list

I need to convert a ta开发者_运维知识库ble 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?


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.


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

This is a decent one actually.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜