Windows 是否有一个程序可以对Lua程序输出的值进行排序?
我需要一个程序(适用于windows),该程序可以按字母顺序对文件中的Lua值进行排序,该文件在Lua程序执行并关闭后保存。我必须不断地合并两个这样的文件,每次在运行比较软件之前手动对它们进行排序是一件痛苦的事情。如果可能的话,一个不需要Lua来工作的人 文件结构如下所示:Windows 是否有一个程序可以对Lua程序输出的值进行排序?,windows,sorting,lua,compare,Windows,Sorting,Lua,Compare,我需要一个程序(适用于windows),该程序可以按字母顺序对文件中的Lua值进行排序,该文件在Lua程序执行并关闭后保存。我必须不断地合并两个这样的文件,每次在运行比较软件之前手动对它们进行排序是一件痛苦的事情。如果可能的话,一个不需要Lua来工作的人 文件结构如下所示: SavedVars = { ["1"] = { ["Val1"] = true, ["Val2"] = true, ["Val3"] = false,
SavedVars = {
["1"] = {
["Val1"] = true,
["Val2"] = true,
["Val3"] = false,
...
["ValX"] = true,
},
["2"] = {
["Val1"] = true,
["Val2"] = true,
["Val3"] = false,
...
["ValX"] = true, },
["X"] = {
["Val1"] = true,
["Val2"] = true,
["Val3"] = false,
...
["ValX"] = true, },
}
SavedStats = {
["1"] = {
["Val1"] = 0,
["Val2"] = 1,
["Val3"] = 55,
...
["ValX"] = -55,
},
["2"] = {
["Val1"] = 0,0005,
["Val2"] = -0,0000000007648,
["Val3"] = 4,
...
["ValX"] = true, },
["X"] = {
["Val1"] = 0,
["Val2"] = 0,
["Val3"] = 0,
...
["ValX"] = 0, },
}
更改Lua程序以按顺序输出内容 我不确定你是用什么来输出这个,我假设像,添加了缩进 您只需将成对k,v(o)do的
更改为成对k,v(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中加载输入,然后使用此序列化方法再次输出它们。以下程序执行此操作(使用上面的serialize_sorted方法):
这可以创建与您的问题类似的文件,即使使用缩进,但使用正确的逗号
如果您有一个同时包含字符串和数字键的表,这种排序就不起作用了,那么您必须考虑如何对它们进行相对排序,并传递一个比较器函数。更改Lua程序以按排序顺序输出内容
我不确定你是用什么来输出这个,我假设像,添加了缩进
您只需将成对k,v(o)do的更改为成对k,v(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中加载输入,然后使用此序列化方法再次输出它们。以下程序执行此操作(使用上面的serialize_sorted方法):
这可以创建与您的问题类似的文件,即使使用缩进,但使用正确的逗号
如果您有一些同时包含字符串和数字键的表,那么这种排序就不起作用了,您必须考虑如何相对地对它们进行排序,并传递一个比较器函数。Lua,而不是Lua。看,Lua解决方案会简单得多。只需加载这两个文件并合并或比较表。定义“排序”的含义如果您想编写工具,欢迎使用stackoverflow。如果您正在查找现有工具,请选中superuser.comPlease并包含所需的“所需输出”。您现在正在手动执行的操作。很有可能为您执行“合并”的Lua代码,从字面上看,少于10行。Lua,而不是Lua。看,Lua解决方案会简单得多。只需加载这两个文件并合并或比较表。定义“排序”的含义如果您想编写工具,欢迎使用stackoverflow。如果您正在查找现有工具,请选中superuser.comPlease并包含所需的“所需输出”。您现在正在手动执行的操作。很可能为您执行“合并”的Lua代码实际上不到10行。