LogicMachine Forum
dynamic name for a table - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: dynamic name for a table (/showthread.php?tid=6228)



dynamic name for a table - CristianAgata - 21.12.2025

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


RE: dynamic name for a table - admin - 21.12.2025

Create a table with tables inside of each key Smile


RE: dynamic name for a table - CristianAgata - 21.12.2025

(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


RE: dynamic name for a table - admin - 21.12.2025

Code:
t = {}
for i = 1, 10 do
  t[ i ] = {}
end

log(t[ 2 ])



RE: dynamic name for a table - CristianAgata - 21.12.2025

(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


RE: dynamic name for a table - CristianAgata - 21.12.2025

(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])



RE: dynamic name for a table - admin - 23.12.2025

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])