-2

I already have a lua-table A={}, A.B={} under global variable. I have a function on the call of what creates lua table D1={}, D1.D2={}, but the porblem is this function places the table in global variable list. When i print all lua values, it prints: A={}, A.B={}, D1=={}, D1.D2={}. Is there a way I can create the function under table under A={}, A.B={} which mean i want output as: A={}, A.B={}, A.B.D1=={}, A.B.D1.D2={}. I dont want to use table.insert() since the hirarchy of source-table is not known.

Darshan Nair
  • 323
  • 1
  • 5
  • 11

1 Answers1

2

It sounds like what you want to do here, is pass the table to the function that creates D1 and D1.D2 so you can append those values wherever you want them.

function addTable(tbl)
    tbl.D1 = {}
    tbl.D1.D2 = {'test'}
end

addTable(A.B)
-- now you can call A.B.D1.D2
print(A.B.D1.D2[1]) -- prints 'test'
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49
  • You don't need to do the assignment in pieces either. You can just use `tbl.D1 = { D2 = {'test'} }` in the body of addTable. – Etan Reisner Jul 09 '13 at 14:32