C2G
C2G

Reputation: 1

How to convert a string to a table in the Solar 2D

I need to convert "{table = {}}" to a table.

How to do it in solar 2d

The code below does not work. Tell me what's wrong with it

Please help me find a solution

actionTable = {projectsSettings = {projects = {"Default"}},settings = {projectName = "Default"},blocks = {Default = {ifTouch = {quantityBlocks = "0"},ifStart = {quantityBlocks = "0"}}}}

local function tableToString(tbl)
    local result = "{"
    for k, v in pairs(tbl) do
        -- Check the key type (ignore any numerical keys - assume its an array)
        if type(k) == "string" then
            result = result..k.." = "
        end

        -- Check the value type
        if type(v) == "table" then
            result = result..tableToString(v)
        elseif type(v) == "boolean" then
            result = result..tostring(v)
        else
            result = result.."\\".."\""..v.."\\".."\""
        end
            result = result..","
    end
    -- Remove leading commas from the result
    if result ~= "" then
            result = result:sub(1, result:len()-1)
    end
    return tostring(result.."}")
end
local str1 = tableToString(actionTable)
print(str1.." STR")
tableToString(actionTable)
local tbl1 = loadstring("return "..str1)
print(tbl1["settings"]["projectName"])

Upvotes: 0

Views: 143

Answers (1)

koyaanisqatsi
koyaanisqatsi

Reputation: 2793

Execute the Function produced by loadstring()...

$ lua5.1
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> str = "{tab = {}}"
> t = loadstring("return " .. str)
> return t
function: 0x566894a0
> t = t() -- Tadah!
> return t
table: 0x56689620
> return t["tab"]
table: 0x566897c0

Than you can understand...

> t = loadstring("return" .. str)() -- Produce changeable global
> return t["tab"]
table: 0x5668a030
> return loadstring("return" .. str)()["tab"] -- Get specific Data
table: 0x56689140

So your Code is working with a little change...

actionTable = {projectsSettings = {projects = {"Default"}},settings = {projectName = "Default"},blocks = {Default = {ifTouch = {quantityBlocks = "0"},ifStart = {quantityBlocks = "0"}}}}

local function tableToString(tbl)
    local result = "{"
    for k, v in pairs(tbl) do
        -- Check the key type (ignore any numerical keys - assume its an array)
        if type(k) == "string" then
            result = result .. k .. " = "
        end

        -- Check the value type
        if type(v) == "table" then
            result = result .. tableToString(v)
        elseif type(v) == "boolean" then
            result = result .. tostring(v)
        else
            result = result .. ('"%s"'):format(v) -- Corrected
        end
            result = result .. ","
    end
    -- Remove leading commas from the result
    if result ~= "" then
            result = result:sub(1, result:len()-1)
    end
    return tostring(result.."}")
end

local str1 = tableToString(actionTable)

print(str1) -- Check

-- tableToString(actionTable) -- Not necessary

local tbl1 = loadstring("return " .. str1)() -- Corrected

print(tbl1["settings"]["projectName"])

Upvotes: 0

Related Questions