Use ipairs for the helper functions

This commit is contained in:
Celtic Minstrel 2015-09-29 12:55:31 -04:00
parent 376b8ac76b
commit 7fb0831da6

View file

@ -39,9 +39,7 @@ end
--! If @a id is not nil, the "id" attribute of the subtag has to match too.
--! The function also returns the index of the subtag in the array.
function helper.get_child(cfg, name, id)
-- ipairs cannot be used on a vconfig object
for i = 1, #cfg do
local v = cfg[i]
for i,v in ipairs(cfg) do
if v[1] == name then
local w = v[2]
if not id or w.id == id then return w, i end
@ -53,11 +51,10 @@ end
--! (Indices start at 1, as always with Lua.)
--! The function also returns the index of the subtag in the array.
function helper.get_nth_child(cfg, name, n)
for i = 1, #cfg do
local v = cfg[i]
for i,v in ipairs(cfg) do
if v[1] == name then
n = n - 1
if n == 1 then return v[2], i end
if n == 0 then return v[2], i end
end
end
end
@ -65,8 +62,7 @@ end
--! Returns the number of subtags of @a with the given @a name.
function helper.child_count(cfg, name)
local n = 0
for i = 1, #cfg do
local v = cfg[i]
for i,v in ipairs(cfg) do
if v[1] == name then
n = n + 1
end