Search code examples
dictionaryluaroblox

Edit table data through a directory in lua


I have a table and a directory :

--directory
{
[1] = "File"
[2] = "Documents"
[3] = "txt_Page"
}

--Table for directory
{
Files = {
    File = {
        Documents = {
            txt_Page = "new"
            }
        }
    }
}

I want to edit the txt_page through the directory and return the newly edited files table but I've tried methods and I can't seem to find it.


Solution

  • Just concat it:

    tabl = {"File", "Documents", "txt_page"}
    path = table.concat(tabl, "/")
    print(path)  -- "File/Documents/txt_page"
    

    For Lua table:

    local new_txt_Page = 3
    local str = "Files['" .. table.concat(tabl, "']['") .. "'] = " .. new_txt_Page
    print (str) -- Files['File']['Documents']['txt_Page'] = 3 
    load(str)() -- execute the string
    

    If your strings have no spaces dashes etc., then you can make it just like:

    str = "Files."..table.concat(tabl, ".").." = ".. new_txt_Page
    print (str) -- Files.File.Documents.txt_Page = 3
    load(str)()