How Can I Use the value of a variable as a table key

Hello, I am having some issues with some test game I am working with .

I want to store the instance id generated by a factory with some values . But I want the factory id generated to be the key of the table . I also want to use the value of a variable to access a key value from a table .

Let me write some code / pseudocode to explain what I mean ( I know lua doesn’t have the dollar sign for variables, I am just adding it to make the code more readable to understand what I am trying to achieve )

$myTable1={}
$myTable2={}
$myTable2.subTable1={subSub1="hello1",subSub2="hello2"}
$myAccessKey="subSub1" ---in my code this value is unpredictable, it can either be subSub1 or subSub2 , its random generated
$instance_id=factory.create("#myFactory",$position,nil,nil,1)
-- I want to set a new key for table 1 as the value of $instance_id , e.g instance1
--I want  to use the variable value as a key of the table 
$myTable1.$instance_id={name=$myTable2.$myAccessKey}
--this should "translate" to for example $myTable.instance23={name=$myTable2.subSub1}
-- which pprint($myTable) should further translate to something like :
-- {instance23:{name:"hello1"}}

I also know that the result of $instance_id would be an hex value , so I can’t use that has a key, but I can easily do hash_to_hex()

Please how can I achieve this, I would appreciate any pointers

If I understand your problem correctly, what you need are some square brackets [ ].

local a = {abc=123, def=456}
local string_var = "def"
print(a[string_var])

This outputs 456.

1 Like

Actually, you can use a hash value (like your $instance_id) as a key just fine.

What you can’t use (in any useful way) is a full URL.

1 Like