표를 쉼표로 구분 된 목록으로 변환하여 텍스트 파일로 저장해야합니다. 루아에서이 작업을 수행 할 수있는 메소드가 내장되어 있습니까?루아 - 표를 쉼표로 구분 된 목록으로 변환
7
A
답변
2
내장 기능이 없지만 웹에 예제가 있습니다. 테이블 배열 인 경우
0
내장 된 방법은 없지만 직접 빌드하려는 경우에는 비교적 쉬운 옵션이 많이 있습니다. 여기에 함께 넣어하는 방법을 알아낼 도움이되는 몇 가지 링크가 있습니다 :
http://www.lua.org/pil/12.1.html
http://lua-users.org/wiki/TableSerialization
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
을.
여기에 사용 된 밑줄은 무엇입니까? pair (table)가 키와 값을 반환하기 때문에 _, p 쌍 (tt)에 대해 s = s .. ","escapeCSV (p)endend – clua7
당신은 열쇠에 관심이 없습니다. – Alan
외부 리소스로만 연결하는 대신 대답 전체를 입력하십시오. –