Reputation: 13
I'm trying to initialize a table of default properties for my script, and I'm facing the following scenario:
_property_table = {
k1 = "val1",
k2 = "val2",
k3 = k1 .. "val3",
}
print(_property_table.k3)
When trying to concatenate k1 within the table constructor, lua fails with the following error:
_impl error: [string "main.lua"]:10: attempt to concatenate global 'k1' (a nil value)
Is this behavior expected, or am I missing something?
I'm fairly new to Lua, so any tips or suggestions on how to proceed would be appreciated.
Thanks
Upvotes: 1
Views: 116
Reputation: 10452
That behaviour is expected: k1
is not a variable name, k1 =
is simply a shortcut for ["k1"] =
inside a table expression. There are two basic solutions:
Use a variable:
local k1 = "val1"
_property_table = {
k1 = k1,
k2 = "val2",
k3 = k1 .. "val3",
}
Assign k3
after the table creation:
_property_table = {
k1 = "val1,
k2 = "val2",
}
_property_table.k3 = _property_table.k1 .. "val3"
Upvotes: 2