Permalink
Browse files

change program to work as filter.

  • Loading branch information...
1 parent 033c647 commit 7210e9e032ad51f5d4805ac52af2c17112be1133 Paŭlo Ebermann committed May 11, 2011
Showing with 107 additions and 36 deletions.
  1. +63 −36 lua/sorted-output.lua
  2. +44 −0 lua/testTable.lua
View
@@ -1,22 +1,35 @@
--- from http://www.lua.org/pil/12.1.1.html, a bit modified
--- and splitted to two met
-function serialize_sorted (o, indent)
- if type(o) == "number" then
+-- 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( o and "true" or "false" )
+ 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)
+ io.write("[")
+ serialize_sorted(k, subindent, cmp)
io.write("] = ")
- serialize_sorted(v, subindent)
+ serialize_sorted(v, subindent, cmp)
io.write(",\n")
end
io.write(indent .. "}")
@@ -26,44 +39,58 @@ function serialize_sorted (o, indent)
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
+ i = i + 1
+ if a[i] == nil then return nil
+ else return a[i], t[a[i]]
+ end
+ end
return iter
end
+-- 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
+
-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,
- },
-}
-io.write("SavedVars = ")
-serialize_sorted(testTable, "")
+
+-- 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
View
@@ -0,0 +1,44 @@
+
+
+
+SavedVars = {
+ ["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,
+ },
+}
+
+SavedStats = {
+ ["1"] = {
+ ["Val3"] = 55,
+ ["ValX"] = -55,
+ ["Val1"] = 0,
+ ["Val2"] = 1,
+ },
+ ["2"] = {
+ ["ValX"] = true,
+ ["Val1"] = 0.0005,
+ ["Val3"] = 4,
+ ["Val2"] = -0.0000000007648,
+ },
+ ["X"] = {
+ ["Val3"] = 0,
+ ["Val1"] = 0,
+ ["ValX"] = 0,
+ ["Val2"] = 0,
+ },
+}

0 comments on commit 7210e9e

Please sign in to comment.