2011-07-05 7 views

답변

2

내장 기능이 없지만 웹에 예제가 있습니다. 테이블 배열 인 경우

This is a decent one actually.

+1

여기에 사용 된 밑줄은 무엇입니까? pair (table)가 키와 값을 반환하기 때문에 _, p 쌍 (tt)에 대해 s = s .. ","escapeCSV (p)endend – clua7

+1

당신은 열쇠에 관심이 없습니다. – Alan

+0

외부 리소스로만 연결하는 대신 대답 전체를 입력하십시오. –

0

아니, 이것에 대한 기능 "내장"없다. 그러나 스스로하는 것은 어렵지 않습니다. 나는 루아 (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 
11

, 당신은 CSV를 인쇄 할 table.concat를 사용할 수 있습니다

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

출력 10,20,30을.

관련 문제