This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

dynamic name for a table
#1
Good morning,

I need a suggest how do this:
I would like create some tables but the names must be dynamic, I will try to explain better with an example.

my_table = {}

for i = 1, 10,1 do
   (here i need to create 10 new tables)
end

the result I would like have could be this

my_table_1,  my_table_2, my_table_3,...  my_table_10.

I wish to explain myself.

BR Cristian
Reply
#2
Create a table with tables inside of each key Smile
Reply
#3
(21.12.2025, 11:07)admin Wrote: Create a table with tables inside of each key Smile

Thanks admin,
Could be a solution, sorry but I'm a little bit confused ?.
Can you share an example?
BR Cristian
Reply
#4
Code:
t = {}
for i = 1, 10 do
  t[ i ] = {}
end

log(t[ 2 ])
Reply
#5
(21.12.2025, 11:37)admin Wrote:
Code:
t = {}
for i = 1, 10 do
  t[ i ] = {}
end

log(t[ 2 ])

Thanks but I don't explain myself very well, this my situation:
I have
name = {'Tv_1','Tv_2','Tv_3'}
number_entry = 3
entry = {[1] = {['component'] = 'switch'}, [2] = {['component'] = 'audioVolume'}, [3] = {['component'] = 'audioMute'}}

result must be a table as this: 

new = {{[Tv_1] = {['component'] = 'switch',['component'] = 'audioVolume',['component'] = 'audioMute'}, [Tv_2] = {['component'] = 'switch',['component'] = 'audioVolume',['component'] = 'audioMute'}, [Tv_3] = {['component'] = 'switch',['component'] = 'audioVolume',['component'] = 'audioMute'}}

in this way in a second part of script when I will call 

object = new.Tv_1[1] I will give switch

Best regards Cristian
Reply
#6
(21.12.2025, 13:13)CristianAgata Wrote:
(21.12.2025, 11:37)admin Wrote:
Code:
t = {}
for i = 1, 10 do
  t[ i ] = {}
end

log(t[ 2 ])

Thanks but I don't explain myself very well, this my situation:
I have
name = {'Tv_1','Tv_2','Tv_3'}
number_entry = 3
entry = {[1] = {['component'] = 'switch'}, [2] = {['component'] = 'audioVolume'}, [3] = {['component'] = 'audioMute'}}

result must be a table as this: 

new = {{[Tv_1] = {['component'] = 'switch',['component'] = 'audioVolume',['component'] = 'audioMute'}, [Tv_2] = {['component'] = 'switch',['component'] = 'audioVolume',['component'] = 'audioMute'}, [Tv_3] = {['component'] = 'switch',['component'] = 'audioVolume',['component'] = 'audioMute'}}

in this way in a second part of script when I will call 

object = new.Tv_1[1] I will give switch

Best regards Cristian
Hi, after several test I have found this solution. Do you think could works?
Code:
name = {'Tv_1','Tv_2','Tv_3'}

entry = {[1] = {['component'] = 'switch'}, [2] = {['component'] = 'audioVolume'}, [3] = {['component'] = 'audioMute'}}

new_t = {}

for _,value in ipairs(name) do
  new_t[value] = {}
    table.insert(new_t[value], entry)
end
--storage.set('my_new_table', new_t)

log(new_t['Tv_1'][1][1])
Reply
#7
If you get the result you want then it's fine. Not sure if you need another table (new_t['Tv_1'][1][1]), it might be enough just to do this:
Code:
for _,value in ipairs(name) do
  new_t[value] = entry
end

log(new_t['Tv_1'][1])
Reply


Forum Jump: