가 정렬 된 순서로 출력에 물건을 당신의 루아 프로그램을 변경
.
나는 이것을 출력하기 위해 무엇을 사용하는지 잘 모르겠다. 나는 들여 쓰기가 추가 된 serialization function in "Programming in Lua"과 같은 것을 가정하고있다.
기능이 chapter 19.3 인 경우에만 for k,v in pairs(o) do
을 for k,v in pairsByKeys(o) do
으로 변경하면됩니다. 여기에 여러분이 준 것과 같은 것을 출력하는 완전한 예제가 있습니다.
-- serializes some object to the standard output.
--
-- o - the object to be formatted.
-- indent - a string used for indentation for tables.
-- cmp - a comparison function to sort the subtables.
-- May be nil, then we sort alphabetically (strings)
-- or numerically (numbers).
--
-- from http://www.lua.org/pil/12.1.1.html, modified to include
-- indentation and sorting.
--
function serialize_sorted (o, indent, cmp)
if type(o) == "nil" then
-- this should not really happen on recursion, as nil can
-- be neither key nor value in a table.
io.write("nil")
elseif type(o) == "number" then
io.write(o)
elseif type(o) == "string" then
io.write(string.format("%q", o))
elseif type(o) == "boolean" then
io.write(tostring(o))
elseif type(o) == "table" then
io.write("{\n")
local subindent = indent .. " "
for k,v in pairsByKeys(o) do
io.write(subindent)
io.write("[")
serialize_sorted(k, subindent, cmp)
io.write("] = ")
serialize_sorted(v, subindent, cmp)
io.write(",\n")
end
io.write(indent .. "}")
else
error("cannot serialize a " .. type(o))
end
end
-- iterates over a table by key order.
--
-- t - the table to iterate over.
-- f - a comparator function used to sort the keys.
-- It may be nil, then we use the default order
-- for strings or numbers.
--
-- from http://www.lua.org/pil/19.3.html
--
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator counter
local iter = function() -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
-- our unsorted test table
testTable = {
["2"] = {
["Val1"] = true,
["ValX"] = true,
["Val2"] = true,
["Val3"] = false,
},
["1"] = {
["ValX"] = true,
["Val1"] = true,
["Val2"] = true,
["Val3"] = false,
},
["X"] = {
["Val3"] = false,
["ValX"] = true,
["Val1"] = true,
["Val2"] = true,
},
}
-- the output.
io.write("SavedVars = ")
serialize_sorted(testTable, "")
프로그램을 변경할 수 없으면 입력을 Lua에로드 한 다음이 직렬화 방법으로 다시 출력 할 수 있습니다. 이 질문에 같은 파일을 만들 수 있습니다
-- loads a string to a table.
-- this executes the string with the
-- environment of a new table, and then
-- returns the table.
--
-- The code in the string should not need
-- any variables it does not declare itself,
-- as these are not available on runtime.
-- It runs in a really empty environment.
function loadTable(data)
local table = {}
local f = assert(loadstring(data))
setfenv(f, table)
f()
return table
end
-- read input from stdin
local data = io.read("*all")
-- load table
local testTable = loadTable(data)
-- output everything
for k, v in pairsByKeys(testTable) do
io.write(k .. " = ")
serialize_sorted(v, "")
io.write("\n")
end
도 들여 쓰기,하지만 바로 쉼표로 다음 프로그램은 (위의 serialize_sorted 방법을 사용)이 작업을 수행합니다.
문자열과 숫자 키가있는 테이블이있는 경우이 정렬이 작동하지 않습니다. 그런 다음 상대적으로 정렬하고 비교 함수를 전달하는 방법을 고려해야합니다.
* 루아 *, 아니 * LUA *. http://www.lua.org/about.html#name – lhf
루아 솔루션은 훨씬 간단합니다. 두 파일을로드하고 테이블을 병합하거나 비교하면됩니다. – lhf
"sort"로 정의하는 것을 정의하십시오 –