A Hamburgler
A Hamburgler

Reputation: 79

Convert array to string in LUA

Trying to convert this to a string customLog2 = {} Which will really look like Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} } I tried this

local Data = string.format( "LogBook = %s ", customLog2 )

But Because CustomLog is an array and not a string or a number I cant insert it. Im trying to get the array into a string for this VariableFile:write(Data) So if anyone can help that would be awesome thanks.

So I want my Output to look like this "local Data = string.format( "LogBook = %s ", customLog2 )" so I can use :write then in my newly created file it should look like this Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} }

So this function works expect one thing.

function TableSerialization(t, i)
    local text = "{\n"
    local tab = ""
    for n = 1, i + 1 do                                                                 --controls the indent for the current text line
        tab = tab .. "\t"
    end
    for k,v in pairs(t) do
        if type(k) == "string" then
            text = text .. tab .. "['" .. k .. "'] = "
        else
            text = text .. tab .. "[" .. k .. "] = "
        end
        if type(v) == "string" then
            text = text .. "'" .. v .. "',\n"
        elseif type(v) == "number" then
            text = text .. v .. ",\n"
        elseif type(v) == "table" then
            text = text .. TableSerialization(v, i + 1)
        elseif type(v) == "boolean" then
            if v == true then
                text = text .. "true,\n"
            else
                text = text .. "false,\n"
            end
        elseif type(v) == "function" then
            text = text .. v .. ",\n"
        elseif v == nil then
            text = text .. "nil,\n"
        end
    end
    tab = ""
    for n = 1, i do                                                                     --indent for closing bracket is one less then previous text line
        tab = tab .. "\t"
    end
    if i == 0 then
        text = text .. tab .. "}\n"                                                     --the last bracket should not be followed by an comma
    else
        text = text .. tab .. "},\n"                                                    --all brackets with indent higher than 0 are followed by a comma
    end
    return text
end

My input array lets say looks like this Log = { Group = WestAPC } now this does not work because WestAPC is not a string but if WestAPC looks like this "WestAPC" it works. I need it to not be in string form.

Upvotes: 5

Views: 19916

Answers (1)

brianolive
brianolive

Reputation: 1671

To be clear, customLog is a table - that is, an associative array of key value pairs. Here’s an easy way to iterate over all key/value pairs and concatenate the pairs into one string:

s = ""

t = {"a", "b", "c", 123, 456, 789} -- sample table
t.someKey = "some value" -- just an extra key value, to show that keys can be strings too

for k, v in pairs(t) do
    s = s .. k .. ":" .. v .. "\n" -- concatenate key/value pairs, with a newline in-between
end

print(s)

Of course, if the value of a key is another table {}, then you will need some extra logic to recursively iterate over these nested tables. I’ll leave that for you as an exercise :)

EDIT 1: Print table as string, showing variable values

s = ""
local ID = 123
local Numbers = 456
local Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} }

s = s .. "{"
for k, v in next, Log do
    s = s .. "{"

    for vk, vv in next, v do
        if next(v, vk) ~= nil then
            s = s .. vk .. " = " .. vv .. ", "
        else
            s = s .. vk .. " = " .. vv  
        end
    end

    if next(Log, k) ~= nil then
        s = s .. "}, "
    else
        s = s .. "}"
    end

end
s = s .. "}"

print(s)

EDIT 2: Print table as string, showing variable names

s = ""
local ID = 123
local Numbers = 456
local Log = { {Group = ID, Pos = Numbers}, {Group = ID, Pos = Numbers} }

s = s .. "{"
for k, v in next, Log do
    s = s .. "{"

    i = 1
    for vk, vv in next, v do
        name = debug.getlocal(1, i)
        if next(v, vk) ~= nil then
            s = s .. vk .. " = " .. name .. ", "
        else
            s = s .. vk .. " = " .. name
        end
        i = i + 1
    end

    if next(Log, k) ~= nil then
        s = s .. "}, "
    else
        s = s .. "}"
    end

end
s = s .. "}"

print(s)

Upvotes: 5

Related Questions