There are two ways to create tables and populate them.
First you need to create and populate the table immediately using the table constructor. This is done as follows:
tab = { keyone = "first value", -- this will be available as tab.keyone or tab["keyone"] ["keytwo"] = "second value", -- this uses the full syntax }
If you do not know what values you want there in advance, you can first create a table using {} , and then fill it with the [] operator:
tab = {} tab["somekey"] = "some value" -- these two lines ... tab.somekey = "some value" -- ... are equivalent
Please note that you can use the second (dotted) syntactic sugar only if the key is a string that matches the rules "identifier", that is, begins with a letter or underscore and contains only letters, numbers and underscores.
PS : Of course, you can combine two methods: create a table using the table constructor, and then fill in the rest using the [] operator:
tab = { type = 'list' } tab.key1 = 'value one' tab['key2'] = 'value two'
Michal kottman
source share