print table lua

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

    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 .. ",\n"
                elseif not (string.find(output_str,"\n",output_str:len())) then
                    output_str = output_str .. "\n"
                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('\t',depth) .. key .. " = "..tostring(v)
                elseif (type(v) == "table") then
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = {\n"
                    table.insert(stack,node)
                    table.insert(stack,v)
                    cache[node] = cur_index+1
                    break
                else
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = '"..tostring(v).."'"
                end

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

            cur_index = cur_index + 1
        end

        if (size == 0) then
            output_str = output_str .. "\n" .. string.rep('\t',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

4.2
5
Awgiedawgie 440215 points

                                    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

4.2 (5 Votes)
0
3.5
8
Krish 100200 points

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

    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 .. ",\n"
                elseif not (string.find(output_str,"\n",output_str:len())) then
                    output_str = output_str .. "\n"
                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('\t',depth) .. key .. " = "..tostring(v)
                elseif (type(v) == "table") then
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = {\n"
                    table.insert(stack,node)
                    table.insert(stack,v)
                    cache[node] = cur_index+1
                    break
                else
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = '"..tostring(v).."'"
                end

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

            cur_index = cur_index + 1
        end

        if (size == 0) then
            output_str = output_str .. "\n" .. string.rep('\t',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

3.5 (8 Votes)
0
3
1
Awgiedawgie 440215 points

                                    --You need to use this {} and put them in a varible
local Table = {13,"1hihihi",
-- u can even do tis
{122,222}}

3 (1 Votes)
0
3.3
10
Awgiedawgie 440215 points

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

3.3 (10 Votes)
0
4.22
9
Phoenix Logan 186120 points

                                    local myTable = {}

4.22 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
lua print table content lua print table contents print tabel lua table in lua how to get table element lua lua how to get from table lua vim print table intro to tables in lua lua print tables types of table lua lua easily print tables ":" in tables lua lua table in table lua table creation lua print table values lua table functions python print lua table lua ... to table lua table reference lua table.create lua dump a function from table lua function in table dump how to put a table in a table in Lua lua table object lua tables inside of table #table in lua tables in lua lua print table data to console create a table in lua creating tables in lua lua table within table how to make a table in lua script lua long table lua tables in function print lua table contents what is a table field in lua lua print table\ print table function lua lua table -1 lua print table keys How do tables work in LUA what is table called in lua type() what is table called in lua create table lua getting table values lua lua print complete table lua function in table lua pretty print table lua create table from a string lua tables print lua table what are tables lua print table as string lua lua table. lua how to make a table with variables how to create a table in lua lua print content of table how to create table lua table function lua lua print entire table how to print a table and a string lua lua display table lua table how to print table lua printtable lua how to print an entire table in lua table lua table.add lua include table lua write elements lua write array lua get table value lua lua tablees how to get a string out of a table in lua lua print table entries lua get table how to edit an array in lua lua get table entry number reference table key with quotes lua print contents of table lua lua print all table values lua create table with keys lua how many table values are lua indexed table lua table syntax table printer lua lua print dictionary tabels lua what is a table in lua lua table printer lua how to print table how to print an entire table lua how to access an index of a tabel lua lua how to make a table table.create() lua dictionary lua lua get part of list print table value lua lua table only 10 elements lua table iindex lua table iindexing how to rpint tables in lua table + lua table = table + 1 lua getting the length of a mixed table lua print content of table lua lua how to print a table reading data off a table in lua lua print table as string lua is in table What's the use of table.create in lua lua hash table lua print map lua for table how to print a table lua lua lists lua array string index lua table dictionary print a table lua lua how to access content inside table lua dictionary of elements in one variable print a value of a table print the the table of value print abtle lua Unencodable Table lua lua Unencodable Table lua Undecidable Table {} in lua lua tbales how to show the most common number in an index lua print out a table lua table get position lua how to store a variables in a table lua lua tabel lua does table equal table how to change a number in an array lua lua for in table lua get string key from array lua does table equal tabel print table to string lua working with tables list a table lua table of strings in lua geting a specific value from a table in lua lua reading table how to create array from string in lua how to create array form string in lua print a table values in Lua lua indexing tables lua get values fro table lua output diagnostic table to console how to dump table lua how to get different value of a value in lua table print table in lua how to create a dictionary in lua print table lua print a table in lua lua tables indexing lua how to create a table how to print a table in lua how to print the values of table in lua dictionary in lua access elements of table in lua lua write to required table lua table insert change index lua new table tablem in lua manage list using lua lua print first in array lua Variables as array keys how t print index in lua is table and list in lua same how to print table in lua lua table pretty print hash tables in lua table with nil index lua lua element creating an associative table in lua by input defining a dictionary in lua lua list table values table sin lua making tables in lua lua table vs map lua print table lua table values printing all values of a table printing all values of a tabbel hierarchy table lua string in lua tables lua print tabel lua sample x element from array lua array indexes names instead of numbers lua table example creating new objects using the same table in lua lua object to string how to get a table if its a string lua how to extract tables from string lua is lua element in array lua tables example lua define table lua dictionary building tables in lua with proper key building tables in lua lua stringify table table constructers lua formating table in lua data to table form for loop lua lua list of things for table lua how to make a lua variable equal a vec3 tables lua lua how to print a tables contents lua table key value lua make array with keys lua dump table lua table to array how to make a table in lua lua print contents of table
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source