2011-07-05 38 views

Trả lời

2

Không có chức năng tích hợp sẵn, nhưng có các ví dụ trên web.

This is a decent one actually.

+1

dấu gạch dưới được sử dụng cho đây là gì? Đã không nhìn thấy điều này trước đây: cho _, p theo cặp (tt) làm s = s .. "," .. escapeCSV (p) kết thúc – clua7

+1

vì cặp (bảng) trả về một khóa và giá trị, dấu _ bạn không quan tâm đến chìa khóa. – Alan

+0

Vui lòng đặt câu trả lời đầy đủ trong câu trả lời thay vì chỉ liên kết với tài nguyên bên ngoài. –

0

Không, đó không phải là "xây dựng trong" chức năng cho việc này. Nhưng nó không khó để làm điều đó cho mình. Tôi giữ một kịch bản xung quanh để đệ quy viết Lua bảng trực tiếp vào các tập tin như Lua script, mà sau đó có thể được nạp và thực hiện như Lua script.

--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 
11

Nếu bảng của bạn là một mảng, bạn có thể sử dụng để in table.concat CSV:

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

đầu ra 10,20,30.

Các vấn đề liên quan