Table constructor

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)
print ("green: " .. colors.g)
print ("blue: " .. colors[3])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.111