Tables are one of the features of Lua script, that make the scripting language so dynamic & easy to use, as they allow us to easily create tables, insert, remove & sort data. Tables are often comprised of arrays that usually involve keywords - or an index number - & a value. Tables can be accessed using multiple different methods.
Creating Tables
Below I am going to show you how to create various different types of tables/arrays.
Index Tables
method 1: automatically generated index values
localt={1,2,3,4,5,"six",7,"ate",9}-- automatically assigns an index number to each value starting from 1localstr=""-- empty stringfori=1,#tdo-- for 1 to table total, print value of index numberstr=str..t[i]-- add current table index value to existing text data in the str variableifi<#tthenstr=str..", "end-- if i is less than table entry total then insert ", " into the str variableendprint("table data: "..str)-- print the table dataprint("table entry total: "..#(t))-- print total of table entries
method 2: manually created index values
localt={[1]=1,[2]="two",[3]=3,[4]=2*2,[5]="five",}localstr=""-- empty stringfori=1,#tdo-- for 1 to table total, print value of index numberstr=str..t[i]-- add current table index value to existing text data in the str variableifi<#tthenstr=str..", "end-- if i is less than table entry total then insert ", " into the str variableendprint("table data: "..str)-- print the table dataprint("table entry total: "..#(t))-- print total of table entries
Keyword Tables
keywords & values
localt={["hello world"]="hello world",example="this also works",camelCaseExample="this will also work",snake_case_example="as will this"}fork,vinpairs(t)doprint(k..": "..v)endprint("\n...but as you can see it does not necessarily return them in the order they were created")
Index Values and Sub-Arrays
auto generated index entries with sub-arrays containing keywords
localt={{x=100,y=300},{x=200,y=111}}print("x = "..t[1].x..", y = "..t[1].y)
Keywords and Sub-Arrays
keyword tables with index based sub-arrays containing keywords
Below are various table functions & advanced methods for interacting with tables.
Table Concatenation
table concatenation without any formatting (index tables only)
localt={1,2,3,4}print(table.concat(t))
table concatenation with formatting (index tables only)
localt={1,2,3,4}print(table.concat(t,", "))
table concatenation with formatting & specified table index range (index tables only)
localt={1,2,3,4}print(table.concat(t,", ",2,4))
Table Insert
insert a value into index position 2 in the specified table (index tables only)
localt={"one","three"}print("before: "..table.concat(t,", "))table.insert(t,2,"two")-- insert "two" into index position 2print("after: "..table.concat(t,", "))
append a value to the end of the specified table (index tables only)
localt={"one","two","three"}print("before: "..table.concat(t,", "))table.insert(t,"four")-- insert "four" at the end of the tableprint("after: "..table.concat(t,", "))
insert a keyword & value into a table
localt={["one"]=1,["three"]=3}localstr=""-- init str variablefork,vinpairs(t)dostr=(str..v.." ")end-- concatenate dataprint("before: "..str)t["two"]=2-- insert key "two" along with a value into the t tablestr=""-- reset strfork,vinpairs(t)dostr=(str..v.." ")end-- concatenate dataprint("after: "..str)
Quick note: Technically it's possible to mix & match keyword & index tables, but it's cleaner & less confusing all around if you don't. However feel free to use index values inside of sub-arrays if you don't want to use keywords, or keywords in sub-arrays of index tables.
Table Remove
remove entry from table (index table only)
localt={1,2,3,4}print("before: "..table.concat(t,", "))table.remove(t,3)-- remove index value 3 from the tableprint("after: "..table.concat(t,", "))
Quick note: Whenever you insert a new index entry into a table containing index values it will automatically adjust all of the index values; the same thing also happens whenever you remove an entry.
remove keyword entry from specified table
localt={["one"]=1,["two"]=2,["three"]=3}localstr=""fork,vinpairs(t)dostr=(str..v.." ")end-- concatenate dataprint("before: "..str)t["two"]=nil-- remove keyword from table by setting it as nilstr=""fork,vinpairs(t)dostr=(str..v.." ")end-- concatenate dataprint("after: "..str)
Quick note: Mixed data tables will return an error if you try to sort them with the table.sort function, the tables need to contain strings or numbers, & not both at the same time; however you can get around it by inserting the numbers in as strings.
get total amount of entries in index table (current level)
localt={1,2,3,4}print(#t)
get total amount of entries in keyword table (current level)
localt={["one"]=1,["two"]=4/2,["three"]=3}functioncountKeys(tbl)localkeys=0-- init keys variablefork,vinpairs(tbl)do-- iterate through tablekeys=keys+1-- increment key value by 1 per key foundendreturnkeys-- return the total amount of keys foundendprint(countKeys(t))