# Lua Tables

René 1 Comment

Lua tables are the most powerful structure in the Lua language. In fact almost everything in Lua is a table.

Other languages have structures for Arrays, for Classes, etc. In Lua we have tables. And we can adopt most characteristics of the afore mentioned structures with tables.

So let’s have a look on some common scenarios with Lua tables.

## Arrays

Lua tables can be used like arrays. Let’s create an empty table like so:

``local myTable = {}``

Always use local when declaring a new variable. Otherwise the variable is created as a global variable. But we should really try to avoid this.

We can also add some data fields on creation time:

``local myTable = { 3, 8, 12, 255 }``

This table does have four fields now. If you want to use this table like an array, you can address each field like this:

``````local a = myTable[1]		-- a = 3
local b = myTable[3]		-- b = 12``````

The same is possible with strings:

``````local myTable = {"I", "am", "learning", "tables"}
local a = myTable[2]		-- a = "am"
local b = myTable[4]		-- b = "tables"``````

As you can see Lua table fields are one-based – this means the first field is addressed with the index number 1 (not zero, like many other languages do).

## Iteration

Often you have to iterate over the array. For example let’s print the content of every field in your array.

Therefore we need a for-loop and we need the number of fields in the table.

Let’s take the last table from above that contains four strings. The length of a table (number of fields in a table) can be read with a # symbol like this:

``````local myTable = {"I", "am", "learning", "tables"}
local length = #myTable      -- length = 4``````

A for-loop is built like this:

``````for i=a,b do
-- do soemthing here in the loop
end``````

a is the starting index of the table and b is the ending index of the table. i is the current index that gets incremented by one on each iteration. So here is a complete iteration example:

``````local myTable = {"I", "am", "learning", "tables"}
local length = #myTable

for i=1,length do
print( myTable[i] ) 	-- see how we use i to read the value
end``````

The output in the console should look like this:

``````>I
>am
>learning
>tables``````

## Associated Arrays

Associated arrays do use keys being associated to a value. In most cases the key would be a string like this:

``````local myTable = {}
myTable["color1"] = "FF0000"   -- the key here is "color1", the associated value is "FF0000"
myTable["color2"] = "33FD21"   -- the key here is "color2", the associated value is "33FD21"``````

There are three ways to get the value of such an associated „array“:

``````print( myTable["color1"] )
print( myTable[1] )
print( myTable.color1 )``````

The above code would always print FF0000 to the console. They do all address the same field in the table.

The last example with the dot notation is a very convenient way. But it does only work with string associated arrays.

You can also use dot notation to create a key/value association. The corresponding string association will be created automatically like the following example proves:

``````local myTable = {}
myTable.name = "Peter Parker"``````

Now you can use one of the following lines of code to read that field:

``````print( myTable.name )
print( myTable["name"] )``````

But the following code would not work:

``````print( myTable[name] )

-- this would insert the variable called name as a key value. But at this point there is no variable called name declared. So this should work:

local name = "name"
print( myTable[name] )``````

## Insert Into Table

If you want to insert a new value into an existing table you can use:

``table.insert( t, value )``

t is the table to insert the new value into. Example:

``````local myTable = {"I","am","learning"}
table.insert( myTable, "Lua" )``````

This would insert the string “Lua” at the end of the table.

You can also add an optional index, to insert the new value at a certain position in the table:

``````local myTable = {"I","am","learning"}
table.insert( myTable, 2, "Lua" )``````

This would create a table that looks like this:

``{"I","Lua","am","learning"}``

## Remove from Table

Like inserting new values you can also remove values from tables:

``table.remove( t, index )``

t is the table to remove the value from. index is the position in the table. Here is an example:

``````local myTable = {"I","Lua", "am","learning"}
table.remove( myTable, 2 )``````

This would remove the “Lua” value from the table which has been at the index position 2.

## Functions in Tables

Tables can not only store values like strings and numbers. It can store other tables and even functions. Let’s have a look:

``````local myTable = {}
myTable[1] = "Peter Parker"
myTable[2] = function()
print( myTable[1] )
end
myTable[2]()		-- use the brackets here because it is a function``````

The above example would write Peter Parker to the console. But especially the last line does look somewhat ugly. Let’s use the dot notation and everything looks better:

``````local myTable = {}
myTable.name = "Peter Parker"
myTable.PrintName = function()
print( myTable.name )
end
myTable.PrintName()    -- use the brackets here because it is a function``````

As you can see, we can also store functions in tables. Maybe now you can imagine, that a table can get a very powerful and complex structure.

## OOP and Arrays in Lua?

Now I’m going on thin ice: the above code reminds me on object orientation (OOP) with properties and methods …

Why thin ice?

Everytime you mention OOP in the Lua community you always get answers like: OOP in Lua is not possible!!! And they are right.

But there are some principles of OOP we can adopt with Lua. But more in this in a different article.

We also should say Array-like structures, because in Lua there are no arrays – only tables.

As always: the truth lies somewhere in between (when using Lua).

## Conclusion

I hope I could shed some light on tables in Lua for you. I will write more about tables when it comes to writing your own modules.

See you
– René

• ## Dear Fellow!

Please consider to say Thank you