Thread: Lua recursion
View Single Post
Old 05-20-2018, 05:50 AM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

1. You're passing the original table into the recursion loop, which seems to be messing Lua up. IMO it's easier to make a local table in each level of the recursion, return it to the parent, and then read it into the parent's table.

2. You aren't giving the recursive function a proper path, just the folder name.

Code:
path =reaper.GetResourcePath()

function ScanPath(path)
    
    local t = {}
    local subdirindex, fileindex = 0,0    
    local path_child
    repeat
        path_child = reaper.EnumerateSubdirectories(path, subdirindex )
        if path_child then 
            table.insert(t,path_child)
            local tmp = ScanPath(path .. "/" .. path_child)
            for i = 1, #tmp do
                --table.insert(t, path .. "/" .. path_child .. "/" .. tmp[i])
                table.insert(t, tmp[i])
            end
        end
        subdirindex = subdirindex+1
    until not path_child

    repeat
        fn = reaper.EnumerateFiles( path, fileindex )
        if fn then 
            --t[#t+1] = path .. "/" .. fn
            t[#t+1] = fn
        end
        fileindex = fileindex+1
    until not fn
    
    return t
end

local t = ScanPath(path)
Lokasenna is offline   Reply With Quote