有一个程序可以排序Lua程序输出的值? [英] Is there an program that can sort the values of a Lua program output?

查看:173
本文介绍了有一个程序可以排序Lua程序输出的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a program (for windows) that can sort alphabetically the Lua values in the file there are saved after the Lua program is executed and closed. I have to merge 2 such files constantly and it's a pain to sort them manually each time before running the comparison software. If possible one that doesn't need Lua in order to work.

文件结构如下:

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程序,

Change your Lua programs to output the stuff in sorted order.

我不知道你输出的是什么,我假设像序列化函数在Lua编程中,添加了缩进。

I'm not sure what you are using to output this, I'm assuming something like the serialization function in "Programming in Lua", with added indentation.

必须改变 for k,v对(o)do for k,v in pairsByKeys(o)do ,其中包含第19.3节 pairsByKeys / a>。

You only have to change the for k,v in pairs(o) do to for k,v in pairsByKeys(o) do, with the pairsByKeys function from chapter 19.3. Here is a complete example which outputs something like you gave there.

-- 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方法):

If you can't change the programs, you could load the input in Lua and then output them again with this serialization method. The following program does this (using the serialize_sorted method above):

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

这可以在你的问题中创建文件,即使有缩进,但用右侧逗号。

This can create files like in your question, even with indentation, but with right commas.

如果你有一些表与字符串和数字键 - 那么你将不得不考虑如何相对排序他们,并传递一个比较器函数。

This sorting does not work if you have some table with both string and number keys - then you would have to think about how to sort them relatively, and pass a comparator function.

这篇关于有一个程序可以排序Lua程序输出的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆