Question About Iterating Through Nested Groups

Anyone got a more elegant way to do the following code? In this case there are 3 levels of nested groups to iterate through - in the future there could be more levels. This also goes for searching through keys and values for tables within tables…

local blur = function (child) -- Code to apply a fill.blur effect on child end local i,j,k for i=1,menuGroup.numChildren do local child = menuGroup[i] if child.numChildren == nil then blur(child) else for j=1,child.numChildren do local child2 = child[j] if child2.numChildren == nil then blur(child2) else for k=1,child2.numChildren do local child3 = child2[k] if child3.numChildren == nil then blur(child3) end end end end end end

You should be able to make a recursive function, something like:

[lua]local blur = function (child)

    – Code to apply a fill.blur effect on child

end

local function blurGroup(group)

    for i=1,group.numChildren do

        local child = group[i]

        if child.numChildren == nil then

            blur(child)

        else

            blurGroup(child)

        end

    end

end

blurGroup(menuGroup.numChildren)[/lua]

PS. you don’t have to localize i,j,k when they are used in a loop like that.

Thanks very much Jon. I didn’t know you could call a function from within itself like that, recursively. Will try this soon.

You should be able to make a recursive function, something like:

[lua]local blur = function (child)

    – Code to apply a fill.blur effect on child

end

local function blurGroup(group)

    for i=1,group.numChildren do

        local child = group[i]

        if child.numChildren == nil then

            blur(child)

        else

            blurGroup(child)

        end

    end

end

blurGroup(menuGroup.numChildren)[/lua]

PS. you don’t have to localize i,j,k when they are used in a loop like that.

Thanks very much Jon. I didn’t know you could call a function from within itself like that, recursively. Will try this soon.