Difference between revisions of "Lua: Tables"

From Mario Fan Games Galaxy Wiki
m
Line 1: Line 1:
 
{{Lua}}
 
{{Lua}}
 
{{needsattention}}
 
{{needsattention}}
'''Tables''' are the only real complex data structure provided in [[Lua]]. Tables are basically arrays, specifically associative arrays, associating a ''key'' with a corresponding ''value''. Unlike most languages, Lua's tables start at 1 instead of 0. Lua tables can store every other non-nil data type, and even mixed types. The following is a valid table:
 
  
<source lang="lua" enclose="div">example_table = {
+
'''Tables''' compose [[Lua]]'s only complex data structure. As such, they cannot be added together, unless supplied with a [[Lua: Metatables|metatable]].
 +
 
 +
Tables are associative arrays that begin the key index from 1. Values in a table may be of any other non-nil or mixed type, including methods / functions.
 +
 
 +
When a variable is assigned to a table, it may be accessed by the following terms:
 +
* ''table.varname'' and ''table["varname"]'', where ''varname'' is the name of the assigned variable. The latter must be used if the variable name contains otherwise invalid characters, for syntax parsing purposes.
 +
* ''table[x]'', where x is the key's index.
 +
* ''table.function()'', ''table.["function"]()'', or ''table:function()'', to call a function or method (which is simply another type of variable.) Any values added into parenthesis are parameters.
 +
* Tables themselves may be called as a function if they contain the [[Lua: Metamethods|metamethod]] ''__call''.
 +
 
 +
'' ''
 +
== Example code ==
 +
<div style='width: 60%; margin-left: 5%; background-color: #f0f0f0; border: 1px solid #444; padding: 8px;'><source lang="lua" enclose='div'>example_table = {
 
     [1] = "one", -- assigns "one" to first index, regular enumerated tables don't need the index explicitly stated
 
     [1] = "one", -- assigns "one" to first index, regular enumerated tables don't need the index explicitly stated
     [0] = "can't touch this", -- index 0 is legal but Lua's table-based functions will ignore it
+
     [0] = "can't touch this", -- index 0 is legal but table-based functions will ignore it
     [5000] = "huge jump", -- Lua tables' array indexes are sparse, meaning this won't allocate memory for the unused 4999 indices
+
     [5000] = "huge jump", -- Won't allocate memory for unused 4999 indices
     test = "variable key", -- variables may also be used within tables, accessed via 'table.var' or 'table["var"]'
+
     var = "variable key", -- Variables are accessed with 'table.var' or 'table["var"]'
 
     ["$"] = "string key", -- using an explicit string key allows characters that can't be used in variable names
 
     ["$"] = "string key", -- using an explicit string key allows characters that can't be used in variable names
     [true] = false, -- other non-number and non-string datatypes can be used as keys (except nil)
+
     [true] = false, -- other non-number and non-string datatypes can be used as keys, except nil
     subtable = { -- tables can be arbitrarily nested
+
     subtable = { -- Tables may be nested freely
 
         subtable2 = { },
 
         subtable2 = { },
 
         subtable3 = {  
 
         subtable3 = {  
Line 17: Line 28:
 
     },
 
     },
 
     func = function (arg) return arg end, -- functions/methods must be declared in this manner within tables   
 
     func = function (arg) return arg end, -- functions/methods must be declared in this manner within tables   
}</source>
+
}</source></div>
 
 
Values in tables can be accessed in a few ways. One way is via dot syntax: '''table1.key''' will return ''test''. Another is by specifying an integer index: '''table1[3]''' will return ''5'', not ''true'', as ''5'' was explicitly stored in '[3]'. Strings can also used within square brackets: '''table1["what"]''' is exactly the same as '''table1.what'''. However, bracket syntax must be used instead of dot syntax in some cases; this is generally the case if you need an evaluated index, or if your index is not a valid identifier name (if it starts with a number or contains spaces, etc)
 
 
 
Functions inside tables can called in various ways; remember, they are variables, after all. '''table1.testfunc(arg)''' and '''table1["testfunc"](arg)''' are two valid ways to call a function. Functions are also able to use colon syntax, which effectively gives Lua [[Lua: Basic OO|OO]] capabilities. '''table1:testfunc(arg)''' is the same as '''table1.testfunc(table1, arg)'''. Tables can be called like functions if they contain a [[Lua: Metamethods|'''__call''' metamethod]].
 
 
 
''[[Lua: Metatables|Metatables]]'' are normal Lua tables that can be "attached" to other tables. They allow you to define otherwise undefined behavior: normally, adding two tables would result in an error. However, metatables allow you to define exactly how tables should be added, which would then make adding tables a valid operation. Tables can only have one metatable, but that metatable itself can have a metatable, and so on. Tables can even be their own metatables, which is useful for methods.
 

Revision as of 01:13, 6 October 2009

Lua
Lua.gif
Basics
Intermediate
Advanced
XLua
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

 Stub.png This article or section is in need of expansion.

Please add further information.


Tables compose Lua's only complex data structure. As such, they cannot be added together, unless supplied with a metatable.

Tables are associative arrays that begin the key index from 1. Values in a table may be of any other non-nil or mixed type, including methods / functions.

When a variable is assigned to a table, it may be accessed by the following terms:

  • table.varname and table["varname"], where varname is the name of the assigned variable. The latter must be used if the variable name contains otherwise invalid characters, for syntax parsing purposes.
  • table[x], where x is the key's index.
  • table.function(), table.["function"](), or table:function(), to call a function or method (which is simply another type of variable.) Any values added into parenthesis are parameters.
  • Tables themselves may be called as a function if they contain the metamethod __call.

Example code

example_table = {
    [1] = "one", -- assigns "one" to first index, regular enumerated tables don't need the index explicitly stated
    [0] = "can't touch this", -- index 0 is legal but table-based functions will ignore it
    [5000] = "huge jump", -- Won't allocate memory for unused 4999 indices
    var = "variable key", -- Variables are accessed with 'table.var' or 'table["var"]'
    ["$"] = "string key", -- using an explicit string key allows characters that can't be used in variable names
    [true] = false, -- other non-number and non-string datatypes can be used as keys, except nil
    subtable = { -- Tables may be nested freely
        subtable2 = { },
        subtable3 = { 
            subtable4 = { },
        },
    },
    func = function (arg) return arg end, -- functions/methods must be declared in this manner within tables  
}