Search code examples
lualua-table

Table in Table with custom key name


I have to declare a table in a table that will act like this:

table = {'79402d' = {'-5.4','5','1.6'}, '5813g1' = {'3','0.15','18'}} 

So when I loop through it, I can use something similar to table['79402d'][0] to print coordinates.


Solution

  • There are two types of syntax for table constructors. The general form:

    t = { ['key'] = value }
    

    (If the key is a valid identifier)The syntax sugar form:

    t = { key = value }
    

    Here you are mixing them up. Because 79402d isn't a valid identifier (beginning with letters or underscore), you have to use the general form:

    t = {['79402d'] = {'-5.4','5','1.6'}, ['5813g1'] = {'3','0.15','18'}}