Difference between revisions of "Basic lua: Tables"

From The Official Visionaire Studio: Adventure Game Engine Wiki
(Created page with "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....")
 
Line 6: Line 6:
 
| class="i_arrow clickablecell" | [[basic_lua:_Functions|>]]
 
| class="i_arrow clickablecell" | [[basic_lua:_Functions|>]]
 
|}
 
|}
 +
== Creating a table ==
 +
{| class="ts"
 +
|-
 +
! method 1: automatically generated index values !!
 +
|-
 +
| <syntaxhighlight>
 +
local t = {1, 2, 3, 4, 5, "six", 7, "ate", 9} -- automatically assigns an index number to each value starting from 1.
 +
 +
print( "index total = " .. #(t) ) -- print total of table entries
 +
 +
for i = 1, table.maxn(t) do -- for 1 to table total, print value of index number
 +
print( t[i] )
 +
end
 +
</syntaxhighlight> || width="200px" | [[File:lb_tables_001.png|thumb|right|180px|click to enlarge]]
 +
|}
 +
{{toc}}

Revision as of 16:04, 22 September 2014

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.

< Index >

Creating a table

method 1: automatically generated index values
local t = {1, 2, 3, 4, 5, "six", 7, "ate", 9} -- automatically assigns an index number to each value starting from 1.

print( "index total = " .. #(t) ) -- print total of table entries

for i = 1, table.maxn(t) do -- for 1 to table total, print value of index number
 print( t[i] )
end
click to enlarge