Table Literals
Like in Lua, tables are delimited in curly braces.
some_values = {1, 2, 3, 4}some_values = {1, 2, 3, 4}Unlike Lua, assigning a value to a key in a table is done with : (instead of =).
some_values = { name: "Bill", age: 200, ["favorite food"]: "rice"}some_values = { name: "Bill", age: 200, ["favorite food"]: "rice"}The curly braces can be left off if a single table of key value pairs is being assigned.
profile = height: "4 feet", shoe_size: 13, favorite_foods: ["ice cream", "donuts"]profile = height: "4 feet", shoe_size: 13, favorite_foods: ["ice cream", "donuts"]Newlines can be used to delimit values instead of a comma (or both):
values = { 1, 2, 3, 4 5, 6, 7, 8 name: "superman" occupation: "crime fighting"}values = { 1, 2, 3, 4 5, 6, 7, 8 name: "superman" occupation: "crime fighting"}When creating a single line table literal, the curly braces can also be left off:
my_function dance: "Tango", partner: "none"
y = type: "dog", legs: 4, tails: 1my_function dance: "Tango", partner: "none"
y = type: "dog", legs: 4, tails: 1The keys of a table literal can be language keywords without being escaped:
tbl = { do: "something" end: "hunger"}tbl = { do: "something" end: "hunger"}If you are constructing a table out of variables and wish the keys to be the same as the variable names, then the : prefix operator can be used:
hair = "golden"height = 200person = { :hair, :height, shoe_size: 40 }
print_table :hair, :heighthair = "golden"height = 200person = { :hair, :height, shoe_size: 40 }
print_table :hair, :heightIf you want the key of a field in the table to to be result of an expression, then you can wrap it in [ ], just like in Lua. You can also use a string literal directly as a key, leaving out the square brackets. This is useful if your key has any special characters.
t = { [1 + 2]: "hello" "hello world": true}t = { [1 + 2]: "hello" "hello world": true}Lua tables have both an array part and a hash part, but sometimes it’s useful to make a semantic distinction between the two. You can use [ ] instead of { } to explicitly declare a table as an array, doing so will prevent any key-value pairs from being written inside it.
some_values = [1, 2, 3, 4]list_with_one_element = [1, ]some_values = [1, 2, 3, 4]list_with_one_element = [1, ]