-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.lua
More file actions
55 lines (53 loc) · 1.47 KB
/
sample.lua
File metadata and controls
55 lines (53 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- Lua 5.3 Required
-- If a file name is given it will use it to write and read from
-- otherwise it will use a temp file and discard it after execution
local streamingTables = require("streaming-tables")
local tempFile = arg[1] or os.tmpname()
local testTable = {
[2]=50,
[3]=60,
[6]=90.23,
[true]="booleans!",
y=function() return "unsupported types are dropped" end,
"hello!",
["nested table"]={
60,50,40,20,10
}
}
local fd = io.open(tempFile,"wb")
-- code from https://gist.github.com/hashmal/874792
-- edits made for corrections and displaying types
local function tprint (tbl, indent)
if not indent then indent = 0 end
local formatting
local keyType
local valueType
for k, v in pairs(tbl) do
keyType = type(k)
if keyType == "number" then
keyType = math.type(k)
end
valueType = type(v)
if valueType == "number" then
valueType = math.type(v)
end
formatting = string.rep("\t", indent) .. keyType .. "\t" .. tostring(k) .. ":\t"
if type(v) == "table" then
print(formatting)
tprint(v, indent+1)
else
print(formatting .. valueType .. "\t" .. tostring(v))
end
end
end
print("Table Before:")
tprint(testTable)
streamingTables.pack(fd, testTable)
fd:close()
fd = io.open(tempFile, "rb")
print("\n\nTable After:")
tprint(streamingTables.unpack(fd))
fd:close()
if not arg[1] then
os.remove(tempFile)
end