Search
 
SCRIPT & CODE EXAMPLE
 

LUA

print table lua

function print_table(node)
    local cache, stack, output = {},{},{}
    local depth = 1
    local output_str = "{
"

    while true do
        local size = 0
        for k,v in pairs(node) do
            size = size + 1
        end

        local cur_index = 1
        for k,v in pairs(node) do
            if (cache[node] == nil) or (cur_index >= cache[node]) then

                if (string.find(output_str,"}",output_str:len())) then
                    output_str = output_str .. ",
"
                elseif not (string.find(output_str,"
",output_str:len())) then
                    output_str = output_str .. "
"
                end

                -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
                table.insert(output,output_str)
                output_str = ""

                local key
                if (type(k) == "number" or type(k) == "boolean") then
                    key = "["..tostring(k).."]"
                else
                    key = "['"..tostring(k).."']"
                end

                if (type(v) == "number" or type(v) == "boolean") then
                    output_str = output_str .. string.rep('	',depth) .. key .. " = "..tostring(v)
                elseif (type(v) == "table") then
                    output_str = output_str .. string.rep('	',depth) .. key .. " = {
"
                    table.insert(stack,node)
                    table.insert(stack,v)
                    cache[node] = cur_index+1
                    break
                else
                    output_str = output_str .. string.rep('	',depth) .. key .. " = '"..tostring(v).."'"
                end

                if (cur_index == size) then
                    output_str = output_str .. "
" .. string.rep('	',depth-1) .. "}"
                else
                    output_str = output_str .. ","
                end
            else
                -- close the table
                if (cur_index == size) then
                    output_str = output_str .. "
" .. string.rep('	',depth-1) .. "}"
                end
            end

            cur_index = cur_index + 1
        end

        if (size == 0) then
            output_str = output_str .. "
" .. string.rep('	',depth-1) .. "}"
        end

        if (#stack > 0) then
            node = stack[#stack]
            stack[#stack] = nil
            depth = cache[node] == nil and depth + 1 or depth - 1
        else
            break
        end
    end

    -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
    table.insert(output,output_str)
    output_str = table.concat(output)

    print(output_str)
end
Comment

lua print contents of table

for k,v in pairs(table) do
 print(v)  
end
Comment

lua table to string

table.concat(list: table, sep: string, i: integer, j: integer)

local tbl = { 'Hello', 'World', 'this', 'is', 'awesome'}
print(table.concat(tbl, ' '))
-> Hello World this is awesome
Comment

print a table in lua

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end
Comment

lua print all elements table

local people = {
   {
       name = "Fred",
       address = "16 Long Street",
       phone = "123456"
   },
   {
       name = "Wilma",
       address = "16 Long Street",
       phone = "123456"
   },
   {
       name = "Barney",
       address = "17 Long Street",
       phone = "123457"
   }
}

for index, data in ipairs(people) do
    print(index)

    for key, value in pairs(data) do
        print('	', key, value)
    end
end
Comment

lua print table

function print_table(node)
    local cache, stack, output = {},{},{}
    local depth = 1
    local output_str = "{
"

    while true do
        local size = 0
        for k,v in pairs(node) do
            size = size + 1
        end

        local cur_index = 1
        for k,v in pairs(node) do
            if (cache[node] == nil) or (cur_index >= cache[node]) then

                if (string.find(output_str,"}",output_str:len())) then
                    output_str = output_str .. ",
"
                elseif not (string.find(output_str,"
",output_str:len())) then
                    output_str = output_str .. "
"
                end

                -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
                table.insert(output,output_str)
                output_str = ""

                local key
                if (type(k) == "number" or type(k) == "boolean") then
                    key = "["..tostring(k).."]"
                else
                    key = "['"..tostring(k).."']"
                end

                if (type(v) == "number" or type(v) == "boolean") then
                    output_str = output_str .. string.rep('	',depth) .. key .. " = "..tostring(v)
                elseif (type(v) == "table") then
                    output_str = output_str .. string.rep('	',depth) .. key .. " = {
"
                    table.insert(stack,node)
                    table.insert(stack,v)
                    cache[node] = cur_index+1
                    break
                else
                    output_str = output_str .. string.rep('	',depth) .. key .. " = '"..tostring(v).."'"
                end

                if (cur_index == size) then
                    output_str = output_str .. "
" .. string.rep('	',depth-1) .. "}"
                else
                    output_str = output_str .. ","
                end
            else
                -- close the table
                if (cur_index == size) then
                    output_str = output_str .. "
" .. string.rep('	',depth-1) .. "}"
                end
            end

            cur_index = cur_index + 1
        end

        if (size == 0) then
            output_str = output_str .. "
" .. string.rep('	',depth-1) .. "}"
        end

        if (#stack > 0) then
            node = stack[#stack]
            stack[#stack] = nil
            depth = cache[node] == nil and depth + 1 or depth - 1
        else
            break
        end
    end

    -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
    table.insert(output,output_str)
    output_str = table.concat(output)

    print(output_str)
end
Comment

lua print table as string

-- Usage: GetTableString(table) | "depth" is recursive param, ignore it.
-- This function will print the table in formatted way.
function GetTableString(o, depth)
    if type(o) ~= "table" then
        return "Given parameter is not a table."
    end

    local pad = function(str, msg, depth)
        local padding = string.rep(str, depth)
        return padding..msg
    end
    
    depth  = depth or 0
    local tabLen = (depth + 1) * 4
    local displayStr = ""
    
    displayStr = displayStr..pad(" ", "{", 0).."
"
    for k,v in pairs(o) do
        displayStr = displayStr..pad(" ", k.." = ", tabLen)
        
        if type(v) == "table" then
            displayStr = displayStr..DebugUtil.getTableString(v, depth+1)
        else
            displayStr = displayStr..tostring(v).." ("..type(v)..")"
        end
        
        displayStr = displayStr.."
"
    end
    displayStr = displayStr..pad(" ", "}", depth * 4).."
"
    
    return displayStr
end
Comment

PREVIOUS NEXT
Code Example
Lua :: when do true loop on roblox 
Lua :: how to check if table is clear 
Lua :: lua table concanation 
Lua :: lua class example 
Lua :: roblox can I have player animations on the server 
Lua :: roblox lua exploiting rconsole 
Matlab :: save mat file script in matlab directory 
Matlab :: matlab if 
Matlab :: count even and odd numbers in matlab 
Matlab :: display sequence in matlab 
Matlab :: matlab find roots of symbolic polynomial 
Matlab :: BIDS json IntendedFor field examples 
Basic :: basic 
Basic :: online c++ to c converter 
Basic :: cmo ler o dado de um cliente ftp usando vbnet 
Elixir :: elixir reverse list 
Elixir :: elixir check memory usage 
Scala :: scala get file from url as string 
Scala :: scala pattern matching 
Scala :: map function scala 
Excel :: Google Sheets How to Count the Days Between Two Dates 
Excel :: excel and 
Perl :: perl split array into smaller arrays 
Pascal :: pascal error handling 
Powershell :: How to display firewall rule ports with powershell 
Clojure :: Clojure defn 
Assembly :: using shape property in flutter for circular corner 
Assembly :: creating a sparse-file with dd 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: jquery unselect option 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =