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 tables

local Table = {
	["TABLE_info"] = "TABLE_response"
}

for i, v in pairs(Table) do
	print(v)
end
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 tables

local favoritefoods_table = {"hamburger", "spaghetti", "pizza", "potato chips"}
--the table--

for i, v in pairs(favoritefoods_table) do --loop through the table-- 
	print(i) --print the number--
  	print(v) --print the value--
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

table in lua

local myTable = {}
Comment

table lua

    a = {}
    x = "y"
    a[x] = 10                 -- put 10 in field "y"
    print(a[x])   --> 10      -- value of field "y"
    print(a.x)    --> nil     -- value of field "x" (undefined)
    print(a.y)    --> 10      -- value of field "y"
Comment

Lua table functions

Note: [var] -> means var is optional 

table.concat(table, [separator], [start index], [end index (inclusive)])
	Concatenates the strings in the table based on the parameters given.
   	
table.insert(table, [pos], value)
	Inserts a value into the table at specified position.

table.remove(table, [pos])
	Removes the value from the table.

table.sort (table ,[comp])
	Sorts the table based on optional comparator argument.
Comment

PREVIOUS NEXT
Code Example
Lua :: free roblux 
Lua :: table lua 
Lua :: lua how to delete a part for roblox 
Lua :: lua split 
Lua :: svelte template vite 
Lua :: type lua 
Lua :: fivem lua set player bucket 
Lua :: https://web.roblox.com/games/1334669864/Lua-Learning-Bloxy?refPageId=e6fa4d30-3657-463c-b6f0-a32a84183315 
Lua :: What percentage of developers use Lua 
Lua :: when do true loop on roblox 
Lua :: roblox for loop 
Lua :: animation event firing too many times roblox 
Matlab :: anonymous function matlab 
Matlab :: matlab pause code run while simulink finishes 
Matlab :: : in matlab 
Matlab :: save table matlab 
Basic :: hello world in basic 
Basic :: como colocar aspas duplas em vbnet 
Elixir :: elixir map 
Elixir :: elixir check memory usage 
Scala :: scala schemaPayload json 
Scala :: add method to string class scala 
Actionscript :: Application insights powershell 
Excel :: excel auto fit row height 
Perl :: perl read file 
Pascal :: pascal press any key to continue 
Powershell :: Take ownership of a folder 
Gdscript :: godot print enum name 
Assembly :: restart kde 
Assembly :: undefined reference to `cv::inRange 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =