LuaNET, the lua 5.1 api documentation!

Lua-Api

Table

[LUA 5.1] table

Lua's Tables are associative arrays. The element table contains all original lua table functions.

Tables are always passed by reference. If you really want to copy a table, you need to copy all its content.

To initialize a table in lua one uses {}.
Code
mytable={}
print(mytable) -- table: 0032F3B8, the address of the table


As its an associative array (also known as map), you can set values in this table.
Code
mytable={}
mytable["name"]="My Table"
print(mytable["name"]) -- My Table


Because of syntatic sugar you can also access the value of the "name"-field by the following construct.
Code
print(mytable.name) -- My Table


In that way you can access and address values in a table.

To view all keys and its values in a table, you can use the following code snippet.
Code
for key,value in pairs(mytable) do
print(key..": "..tostring(value))
end


If you don't want to set the key and just want to append the value to the table, you can use table.insert.
Code
mytable={}
table.insert(mytable,"This")
table.insert(mytable,"is")
table.insert(mytable,"content")
for key,value in pairs(mytable) do
print(key..": "..tostring(value))
end
--[[
1: This
2: is
3: content
]]


To connect all items in a table to one string there is table.concat.
Code
mytable={}
table.insert(mytable,"This")
table.insert(mytable,"is")
table.insert(mytable,"content")
print(table.concat(mytable,'-')) -- This-is-content

Comments

© 2007 DracoBlue :: Valid XHTML, CSS, RSS Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC | SourceNet © 2007, DracoBlue :: Page created in 0.265 seconds with 54 queries.