2011-05-09 67 views
0

我需要一個能夠按字母順序排序Lua程序執行並關閉後保存的文件中Lua值的程序(對於windows)。我必須不斷地合併2個這樣的文件,每次在運行比較軟件之前手動對它們進行分類是一種痛苦。如果可能的話,那些不需要Lua才能工作的人。有沒有可以對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, }, 
} 
+3

* Lua *,而不是* LUA *。見http://www.lua.org/about.html#name – lhf 2011-05-09 17:07:50

+4

一個Lua解決方案會簡單得多。只需加載兩個文件併合並或比較表格。 – lhf 2011-05-09 17:10:00

+1

定義「排序」的含義 – 2011-05-09 19:55:15

回答

3

改變你的Lua程序輸出的東西在有序。

我不知道你用什麼來輸出這個,我假設像serialization function in "Programming in Lua",增加了縮進。

您只需將for k,v in pairs(o) do更改爲for k,v in pairsByKeys(o) do,pairsByKeys功能chapter 19.3即可。這是一個完整的例子,它輸出的東西就像你在那裏輸出的。

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

這可以在你的問題創造這樣的文件,即使有壓痕,但正確的逗號。

如果你有一些帶有字符串和數字鍵的表,那麼你就不得不考慮如何對它們進行相對排序,並傳遞一個比較函數。