-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstable_pairs.lua
More file actions
59 lines (51 loc) · 1.15 KB
/
Copy pathstable_pairs.lua
File metadata and controls
59 lines (51 loc) · 1.15 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
56
57
58
59
-- https://www.lua.org/manual/5.4/manual.html#pdf-next
-- The order in which the indices are enumerated is not specified, even for numeric indices.
local table_insert = table.insert
local table_sort = table.sort
local opairs = _G.pairs
_G.pairs = function(t)
local st = {}
for k in opairs(t) do
table_insert(st, k)
end
table_sort(st, function(key1, key2)
-- Warning! This function is only guaranteed to work if all keys are strings or numbers.
-- "number" < "string", so numbers will be sorted before strings.
local type1, type2 = type(key1), type(key2)
if type1 ~= type2 then
return type1 < type2
else
return key1 < key2
end
end)
local idx = 0
local function n(t)
while idx <= #st do
idx = idx + 1
local key = st[idx]
if t[key] then
return key, t[key]
end
end
return
end
return n, t, nil
end
---
do
print("example 1 ---")
local t = {[1001] = 1, [1003] = 3, ["abc"] = "string_abc" }
for k, v in pairs(t) do
print(k, v)
end
end
do
print("example 2 ---")
local t = {[1001] = 1, [1003] = 3, ["abc"] = "string_abc" }
for k, v in pairs(t) do
if t["abc"] then
t["abc"] = nil
end
print(k, v)
end
end