lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Michael Gerbracht schrieb:
I would like to create a multidimensional table with a lua fuction containing
a predefined value[1] instead of nil which should work like this:

example = table.dim(10,10,10)
print(example[2][4][3])

output --> 0


Hi Michael,

i would do it with a recursive function instead of loadstrings which are slow and "ugly".
For example:


function table.dim(...)
   local ret={}

   if select("#",...)==1 then
       for i=1,select(1,...) or 1 do
           ret[i]=0
       end
   else
       for i=1,(select(1,...)) or 1 do
           ret[i]=table.dim(select(2,...))
end end return ret
end

local tbl=table.dim(10,20,30)
print (tbl[1][2][3])
print (#tbl,#tbl[1],#tbl[1][2])


Best Regards,
Michael