Parent and children

Tables use a parent-child relation to store values. This relation can be accessed in three ways, which are all quite easy to grasp.

The Curly Bracket Syntax can be used only when defining a new table.

The Dot Syntax and Square Bracket Syntax can be used to declare new elements or access the children of previously declared tables.

 

Curly bracket syntax

The parent elements are declared using the equal sign = followed by curly brackets { } . Example,

The child elements are declared inside the curly brackets { }. Commas , separate children from each other inside the same table. Example,

Note: A comma is not required after the last element in a table, but can be added for ease of maintenance.

 

Basic curly bracket syntax example:

 

In the example below,  ACTIONFAIL is the root parent table, which contains a child element  SHAVESHAVE  is also a table, containing  AWAKEBEEFALO and  GENERIC.

ACTIONFAIL curly bracket example:

 

Dot syntax

The dot syntax can not be used to declare parent elements (create new tables). You must first declare the table some_table = {} , then use the dot syntax to access it’s children.

Child elements of tables can be accessed by the name of the table (parent) followed by a dot. Example,

You can access children of children too (and so on) by appending a dot each time.

Note that child elements starting with a numeric or boolean value can not be accessed with the dot syntax. Use the square bracket syntax to access them.

 

Basic dot syntax example:

 

Using the ACTIONFAIL example from above, we can access the children of  ACTIONFAIL using the dot syntax.

Below, the same example is declared relying mostly on the dot syntax. The parent elements ( ACTIONFAIL and  SHAVE) are declared first using curly brackets, then children elements are declared through the dot syntax.

ACTIONFAIL dot example:

 

Square bracket syntax

Similar to the dot syntax. The difference is that element names are enclosed in square brackets [ ]. The square bracket syntax can also be used to access child elements with numeric or boolean names. Example,

You can access children of children too,

 

Basic square bracket syntax example:

 

ACTIONFAIL square bracket example:

 

Note: Every element can only have one parent, but every parent can have multiple children (and grandchildren, etc).

See Key and Value for more information.