July 2018
Beginner
202 pages
5h 42m
English
If you know the values stored in a table at the time of creating the table, you can use the table constructor to assign the values. Just write the key/variable pairs as assignment statements between the curly braces that define the table. By not including strings, the keys are assumed to be strings:
colors = { red = "#ff0000", green = "#00ff00", blue = "#0000ff"}print ("red: " .. colors.red)print ("green: " .. colors["green"])print ("blue: " .. colors.blue)
Non-string keys can be used if the bracket notation is followed within the constructor. The following code shows valid ways to declare table elements in the table constructor:
colors = { r = "#ff0000", ["g"] = "#00ff00", [3] = "#0000ff"}print ("red: " .. colors.r) ...