Passing a table as argument to function in Lua -


i want loop through different indexed tables passing initial table argument. have table:

local table = { stuff_1 = {     categories = {},     [1] = {          name = 'wui',         time = 300     } }, stuff_2 = {     categories = {'stuff_10', 'stuff_11', 'stuff_12'},     stuff_10 = {         categories = {},         [1] = {             name = 'peo',             time = 150         },         [2] = {              name = 'uik',             time = 15         },         [3] = {              name = 'kpk',             time = 1230         },           [4] = {                  name = 'aer',             time = 5000         }     },     stuff_11 = {         categories = {},         [1] = {              name = 'juio',             time = 600         }     },     stuff_12 = {         categories = {},         [1] = {             name = 'erq',             time = 980         },         [2] = {             name = 'faf',             time = 8170         }     } } 

i wanted make recursive function check if name in of tables equal thing , return string. recursivity lies in idea of updating table whatever ammount i'd (or until limit). don't understand what's wrong since when try:

for k, v in pairs(table)     print(k, v, #v.categories) end  

it correctly prints:

stuff_2 table: 0x10abb0 3 stuff_1 table: 0x10aab8 0 

but when passing table parameter the function below, gives error:

[string "stdin"]:84: attempt length of field 'categories' (a nil value) 

function:

function checkmessage(table)     local = 1     local message = ""      k, v in pairs(table)          if(#v.categories == 0)               while(v[i])                  if(v[i].name == 'opd')                      if(v[i].time ~= 0)                          message = "return_1"                     else                          message = "return_2"                     end                  end                  = + 1              end         else             checkmessage(table[k])         end      end      return message  end 

edit: problem lies in not ignoring when using pairs onto table, doesn't have tables category subtable has table named category, if ignored problem fixed.

you're recursing subtables don't have categories field. trying access categories on them yields nil, try use length operator on. hence error:

  attempt length of field 'categories' (a nil value) 

if can't hand trace app, put in more print statements or line level debugger.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -