Output has written
!Initial Values of test1d
[Table:test1d]
1 = 3
2 = 2
3 = 4
4 = 2
a = 1
d = 3
c = 3
b = 2
!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.
[Table:sorted]
1 = 2
2 = 2
3 = 3
4 = 4
a = 1
d = 3
c = 3
b = 2
!Sorted using ssort to remove none-indices
[Table:sorted]
1 = 2
2 = 2
3 = 3
4 = 4
!The DIFF between test1d and sorted
[Table:(Anonymous)]
removed = {a = 1, d = 3, c = 3, b = 2}
same = {2 = 2}
Tables = {t2 = '', t1 = ''}
changed = {1 = [T], 3 = [T], 4 = [T], a = [T], d = [T], c = [T], b = [T]}
1 = {new = 2, old = 3}
3 = {new = 3, old = 4}
4 = {new = 4, old = 2}
a = {old = 1}
d = {old = 3}
c = {old = 3}
b = {old = 2}
new = {}
!Inverted values of test1d
[Table:test1d]
1 = a
2 = {1 = 2, 2 = 4, 3 = 'b'}
3 = {1 = 1, 2 = 'd', 3 = 'c'}
4 = 3
!Inverted values of inverted values of test1d
[Table:(Anonymous)]
a = 1
{1 = 2, 2 = 4, 3 = 'b'} = 2
3 = 4
{1 = 1, 2 = 'd', 3 = 'c'} = 3
!The DIFF between doubleinverted test1d and sorted
[Table:(Anonymous)]
removed = {a = 1, table: 002C5960 = 2, table: 002C59B0 = 3}
same = {}
Tables = {t2 = 'sorted', t1 = 'Anon:T1'}
changed = {a = [T], table: 002C5960 = [T], table: 002C59B0 = [T], 3 = [T]}
a = {old = 1}
{1 = 2, 2 = 4, 3 = 'b'} = {old = 2}
{1 = 1, 2 = 'd', 3 = 'c'} = {old = 3}
3 = {new = 3, old = 4}
new = {1 = 2, 2 = 2, 4 = 4}
!Inverted values of inverted values of test1d with smart keys
[Table:test1d]
1 = 3
2 = 2
3 = 4
4 = 2
a = 1
d = 3
c = 3
b = 2
!Initial Values of _2d matrix
[Table:_2d]
lee = {score = 1, rank = 3}
joe = {score = 2, rank = 2}
bob = {score = 5, rank = 1}
!_2d with its keys stripped as 'name'
[Table:_2d]
1 = {name = 'lee', score = 1, rank = 3}
2 = {name = 'joe', score = 2, rank = 2}
3 = {name = 'bob', score = 5, rank = 1}
!_2d with its 'score' column set as the keys
[Table:_2d]
1 = {name = 'lee', score = 1, rank = 3}
2 = {name = 'joe', score = 2, rank = 2}
5 = {name = 'bob', score = 5, rank = 1}
!Returns a table of the values of column 'score'
[Table:(Anonymous)]
1 = {key = 1, value = 1}
2 = {key = 2, value = 2}
3 = {key = 5, value = 5}
!Returns a table of the values of column 'rank'
[Table:(Anonymous)]
1 = {key = 1, value = 3}
2 = {key = 2, value = 2}
3 = {key = 5, value = 1}
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
if not _tostring then _tostring = tostring end
local _table = {}
for k, v in pairs(table) do _table[k] = v end
function table.find(t, o)
	for k, v in pairs(t) do
		if v == o then
			return k
		end
	end
end
function table.seperate(t)
	local itab = {}
	local tab = {}
	for k, v in pairs(t) do
		if type(k) == "number" then
			itab[k] = v
		else
			tab[k] = v
		end
	end
	return itab, tab
end
function table.invert(t, _i)
	local tab = {}
	for k, v in pairs(t) do
		if type(v) == "table" and not _i then
			for _k, _v in pairs(v) do
				tab[_v] = k
			end
		else
			if not tab[v] then
				tab[v] = k
			else
				if not (type(tab[v]) == "table") then
					tab[v] = {tab[v], k}
				else
					table.insert(tab[v], k)
				end
			end
		end
	end
	return tab
end
function table.length(t)
	local n = 0
	for k, v in pairs(t) do
		n = n+1
	end
	return n
end
function table.ascend(x, y)
	return x<y
end
function table.descend(x, y)
	return x>y
end
function table.ssort(t,f)
	if not f then f = table.ascend end
	local i=1
	local x, _x = table.seperate(t)
	local n = table.length(x)
	while i<=n do
		local m,j=i,i+1
		while j<=n do
			if f(x[j],x[m]) then m=j end
			j=j+1
		end
		x[i],x[m]=x[m],x[i]			-- swap x[i] and x[m]
		i=i+1
	end
	return table.join(x, _x, false)
end
function table.sort(t, f)
	return table.seperate(table.ssort(t, f))
end
function table.first(t)
	for k, v in pairs(t) do
		return k, v
	end
end
function table.sfirst(t)
	for k, v in pairs(t) do
		if type(k) == "string" then
			return k, v
		end
	end
end
function table.ifirst(t)
	for k, v in pairs(t) do
		if type(k) == "number" then
			return k, v
		end
	end
end
function table.firstVal(t)
	for k, v in pairs(t) do
		return v
	end
end
function table.sfirstVal(t)
	for k, v in pairs(t) do
		if type(k) == "string" then
			return v
		end
	end
end
function table.ifirstVal(t)
	for k, v in pairs(t) do
		if type(k) == "number" then
			return v
		end
	end
end
function table.join(t1, t2, o)
	local tab = t1
	for k, v in pairs(t2) do
		if (t1[k] and o) or not (t1[k]) then
			tab[k] = v
		end
	end
	return tab
end
local seen = {}
local _s_ = ""
function table.dump(t,i, b)
	seen[t]=true
	if not i then i = "" end
	for k,v in pairs(t) do
		local _v, _b = table.tostring(v)
		_s_ = _s_..i.._tostring(table.tostring(k)).." = ".._tostring(_v).." \n"
		if _b and not seen[v] then
			table.dump(v, i.."\t", true)
		end
	end
	if not b then
		local name = table.find(_G, t)
		if not name then name = "(Anonymous)" end
		_s_ = "[Table:"..name.. "]\n".._s_
		print (_s_)
		local r = _s_
		_s_ = ""
		seen = {}
		return r
	end
end
function table.tostring(t)
	if not (type(t) == "table") then if type(t) == "function" then return "fn()" end return _tostring(t) end
	local s = "{"
	local b = false
	for k, v in pairs(t) do
		if type(v) == "string" then v = "'"..v.."'" end
		if type(v) == "function" then v= "fn()" end
		if type(v) == "table" then
			v = "[T]"
			b = true
		end
		s = s .. _tostring(k) .. " = " .. _tostring(v) .. ", "
	end
	s = string.sub(s, 1, #s - 2).."}"
	return s, b
end
function table.stripkeys(t, n)
	local tab = {}
	if not n then n = "key" end
	for k, v in pairs(t) do
		if isTable(v) then
			v[n] = k
			table.insert(tab,v)
		end
	end
	return tab
end
function table.getkeys(t)
	local tab = {}
	for k, v in pairs(t) do
		tab[k] = true
	end
	return tab
end
function table.ismatrix(t)
	local _k, _v = table.first(t)
	if not (type(_v) == "table") then return false end
	local keys = table.getkeys(_v)
	for k, v in pairs(t) do
		if not (type(v) == "table") then return false end
		if not table.havekeys(table.getkeys(v), keys) then return false end
	end
	return true
end
function table.equal(t1, t2)
	for k, v in pairs(t1) do
		if t2[k] ~= v then return false end
	end
	return true
end
function table.havekeys(t1, t2)
	for k, v in pairs(t1) do
		if not t2[k] then return false end
	end
	return true
end
function table.haskey(t, k)
	if not t[k] then return false end
	return true
end
function table.setkey(t, k, p)
	if not table.ismatrix(t) then return t end
	if not table.haskey(table.firstVal(t), k) then return t end
	local tab = {}
	for i, v in pairs(t) do
		tab[v[k]] = v
		if not p then
			tab[v[k]][k] = nil
		end
	end
	return tab
end
function table.get(t, key)
	if not table.ismatrix(t) then return t end
	if not table.haskey(table.firstVal(t), key) then return t end
	local tab = {}
	for k, v in pairs(t) do
		table.insert(tab, {key = k, value = v[key]})
	end
	return tab
end
function table.diff(t1, t2, b)
	local tab1, tab2 = "", ""
	if not b then
		tab1 = table.find(_G, t1)
		tab2 = table.find(_G, t2)
	end
	if not tab1 then tab1 = "Anon:T1" end
	if not tab2 then tab2 = "Anon:T2" end
	local changed = {}
	local removed = {}
	local new = {}
	local same = {}
	for k, v in pairs(t1) do
		if not t2[k] then
			removed[k] = v
		end
		if not (t2[k] == v) then
			changed[k] = {old = v, new = t2[k]}
		end
		if t2[k] == t1[k] then
			same[k] = v
		end
	end
	for k, v in pairs(t2) do
		if not (changed[k] or removed[k] or same[k]) then
			new[k] = v
		end
	end
	return {changed = changed, removed = removed, new = new, same = same, Tables = {t1 = tab1, t2 = tab2}}
end
function isTable(t)
	if type(t) == "table" then return true end
end
--[--
print("!Initial Values of test1d")
test1d = {a = 1, b=2, c=3, d = 3, 3, 2, 4, 2}
table.dump(test1d)
print("!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.")
sorted = table.ssort(test1d)
table.dump(sorted)
print("!Sorted using ssort to remove none-indices")
sorted = table.sort(test1d)
table.dump(sorted)
print("!The DIFF between test1d and sorted")
table.dump(table.diff(test1d, table.sort(test1d)))
print("!Inverted values of test1d")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Inverted values of inverted values of test1d")
table.dump(table.invert(test1d, true))
print("!The DIFF between doubleinverted test1d and sorted")
table.dump(table.diff(table.invert(test1d, true), sorted))
print("!Inverted values of inverted values of test1d with smart keys")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Initial Values of _2d matrix")
_2d = {
	lee = {score = 1, rank = 3},
	joe = {score = 2, rank = 2},
	bob = {score = 5, rank = 1}
}
table.dump(_2d)
print("!_2d with its keys stripped as 'name'")
_2d = table.stripkeys(_2d, "name")
table.dump(_2d)
print("!_2d with its 'score' column set as the keys")
_2d = table.setkey(_2d, "score", true)
table.dump(_2d)
print("!Returns a table of the values of column 'score'")
table.dump(table.get(_2d, "score"))
print("!Returns a table of the values of column 'rank'")
table.dump(table.get(_2d, "rank"))
--]]--