Module:GetJSON: Difference between revisions

From AlternateWiki
Content added Content deleted
No edit summary
No edit summary
Line 23: Line 23:
local function getValueByKeys(root, keys)
local function getValueByKeys(root, keys)
local value = root
local value = root
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 variable
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
-- Iterate through each key
-- Iterate through each key
for _, key in ipairs(keys) do
for _, key in ipairs(keys) do
if key[0] == "#" then
keyval = tonumber(string.sub(key, 2))
i = 1
for name, line in pairsByKeys(value) do
if i == keyval then
return value[name]
end
i = i + 1
end
end
-- Check if the key is numeric
-- Check if the key is numeric
if tonumber(key) then
if tonumber(key) then