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.
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
method 3: 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")
method 4: 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)
method 5: keyword tables with sub-arrays containing keywords containing even more keywords