Advertisement
AlewAlow

sum of table

Oct 1st, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.80 KB | None | 0 0
  1. local function gyat(main)
  2.   local done = {}
  3.   local total = 0
  4.  
  5.   local function tryTable(tbl)
  6.     if done[tbl] then
  7.       return
  8.     end
  9.    
  10.     done[tbl] = true
  11.    
  12.     for k, v in pairs(tbl) do
  13.       if type(v) == "number" then
  14.         total = total + v
  15.       end
  16.      
  17.       if type(v) == "table" then
  18.         tryTable(v)
  19.       end
  20.      
  21.       if type(k) == "table" then
  22.         tryTable(k)
  23.       end
  24.     end
  25.   end
  26.  
  27.   tryTable(main)
  28.   return total
  29. end
  30.  
  31. local Table = {0, 0, 1, 0, 3, 10, 56, true, false, nil, "yo mama"}
  32. print(gyat(Table)) -- 70
  33. --
  34. local Table = {0, {1,2,3}, {0, 10, 1}}
  35. print(gyat(Table)) -- 17
  36. --
  37. local Table = { [{1,2,3}] = {1,2,3} }
  38. print(gyat(Table)) -- 12
  39. --
  40. local Table = { [{1,2,3}] = {1,2,3} }
  41. Table.A = Table
  42. print(gyat(Table)) -- 12
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement