FANDOM


TableEdit

EssentialsEdit

A table is a type of value that contains other values. Lua's tables are associative, meaning they can be indexed with keys and by number. Special tables called Metatables contain Metamethods that control Lua's operators. Also, unlike most other languages, Lua's number indexes start at 1 not 0.

SamplesEdit

Sample
The following is an sample of indexing using a key.

Source:

local foo = {Hello = 'World', Foo = 'Bar'}
print(foo["Hello"])
Output:

World


Sample
The following is an sample of indexing using a number.

Source:

local foo = {'Foo', 'Bar'}
print(foo[2])
Output:

Bar


Sample
The following is a sample of a Generic For.

Source:

for i,v in pairs({'Foo', 'Bar'}) do
       print(v)
end
Output:

Foo       Bar


ExtraEdit

Multi-dimensional ArraysEdit

Tables can also contain other tables that can be indexed. These are called multi-dimensional arrays.

Sample
The following is an example of multi-dimensional arrays.

Source:

local a = {
       foo = {
              'Bar',
              'Baz'
       },
       hello = {
              'World',
              'Users'
       }
}
print(a['foo'][1])
Output:

Bar


Index Syntactic SugarEdit

Sample
The following is an example of syntactic sugar for key indexing.

Source:

local a = {foo = 'Bar'}
print(a.foo)
Output:

Bar

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.