【问题标题】:Is there an program that can sort the values of a Lua program output?是否有可以对 Lua 程序输出的值进行排序的程序?
【发布时间】:2011-08-21 20:36:45
【问题描述】:

我需要一个程序(用于 Windows),它可以在 Lua 程序执行和关闭后保存文件中的 Lua 值按字母顺序排序。我必须不断地合并 2 个这样的文件,每次在运行比较软件之前手动对它们进行排序是很痛苦的。如果可能的话,它不需要 Lua 就可以工作。

文件结构如下:

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,而不是 LUA。见lua.org/about.html#name
  • Lua 解决方案会简单得多。只需加载这两个文件并合并或比较表格即可。
  • 定义“排序”的含义
  • 如果你想写一个工具,欢迎来到stackoverflow。如果您正在寻找现有工具,请查看 superuser.com
  • 请包含您想要的“所需输出”。你现在手动做的那个。从字面上看,为您“合并”的 Lua 代码很可能不到 10 行。

标签: windows sorting lua compare


【解决方案1】:

更改您的 Lua 程序以按排序顺序输出内容。

我不确定你用什么来输出这个,我假设像 serialization function in "Programming in Lua" 一样,添加了缩进。

您只需使用 chapter 19.3 中的 pairsByKeys 函数将 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 中加载输入,然后用这种序列化方法再次输出。以下程序执行此操作(使用上面的 serialize_sorted 方法):

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

这可以创建与您的问题类似的文件,即使带有缩进,但使用正确的逗号。

如果您有一些带有字符串和数字键的表,则这种排序不起作用 - 那么您必须考虑如何对它们进行相对排序,并传递一个比较器函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多