var = "testing"
var = {body = var}
print(var.body)
print(var["body"])
Comma in table construction separates table entries. So by specifying {"body", "testing"}
, you create two entries with keys 1, 2 that are equal to "body" and "testing", respectively, since that is how you initialise a sequential array.
If you want to assign arbitrary keys to each value, you have to use =
, as in {key1 = "body", key2 = "testing"}
. Now you can access those elements in two ways. One is object like, using dot:
tab1.key1
And one is array/map like, using square brackets:
tab1["key1"]
.
In the second example, you have to pay attention to quotes. If you use double quotes, you're accessing a key directly. If not (tab1[key1]
), you're using value stored in a variable named key1
as key specifier.