westor

printTable()

Feb 5th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. local function printTable( t )
  2.  
  3.     local printTable_cache = {}
  4.  
  5.     local function sub_printTable( t, indent )
  6.  
  7.         if ( printTable_cache[tostring(t)] ) then
  8.             print( indent .. "*" .. tostring(t) )
  9.         else
  10.             printTable_cache[tostring(t)] = true
  11.             if ( type( t ) == "table" ) then
  12.                 for pos,val in pairs( t ) do
  13.                     if ( type(val) == "table" ) then
  14.                         print( indent .. "[" .. pos .. "] => " .. tostring( t ).. " {" )
  15.                         sub_printTable( val, indent .. string.rep( " ", string.len(pos)+8 ) )
  16.                         print( indent .. string.rep( " ", string.len(pos)+6 ) .. "}" )
  17.                     elseif ( type(val) == "string" ) then
  18.                         print( indent .. "[" .. pos .. '] => "' .. val .. '"' )
  19.                     else
  20.                         print( indent .. "[" .. pos .. "] => " .. tostring(val) )
  21.                     end
  22.                 end
  23.             else
  24.                 print( indent..tostring(t) )
  25.             end
  26.         end
  27.     end
  28.  
  29.     if ( type(t) == "table" ) then
  30.         print( tostring(t) .. " {" )
  31.         sub_printTable( t, "  " )
  32.         print( "}" )
  33.     else
  34.         sub_printTable( t, "  " )
  35.     end
  36. end
Add Comment
Please, Sign In to add comment