LUA Tables as HashTables and a little bit more

Tables can be used as Hash Tables where each element is associated by a name.

local dimens = {
    height = 10,
    width = 5
}

dimens.depth = 2

You will notice above an additional element is added without having to resize the table or pre-define the element.

print( dimens.depth ) -- 2
print( dimens["depth"] ) -- 2

Advanced Collections
As well as numbers and string a table can hold functions, references to functions as well as other tables.

local products = {
    {code = "Prod1", description = "bulb" , price = 32.50, quantity = 22},
    {code = "Prod2", description = "door" , price = 10.02, quantity = 2}
}

print (products[2]["code"])  -- Prod2
print (products[2].description)   -- door
print (products[2].price)  -- 10.02
print (products[2]["quantity"])  -- 2

Tables can be mixed (Hash Table and Array)
A table can hold array and hash table structures.

local mixed = {
    "alpha",
    "beta",
    ["one"] = "uno",
    ["two"] = "dos"
}

Iterating elements

for key, value in pairs[mixed] do
    print(key, value)
end

The output from the above is:

1, alpha
2, beta
one, uno
two, dos

Tables can contain function references

local function helloWorld()
    print( "Hello World!" )
end

local myTable = {
    name = "Bob",
    func = helloWorld}

myTable.func() -- output: Hello World!

----------

local function helloWorld()
    print( "Hello World!" )
end

local myTable = { 100, 100, helloWorld, true }

myTable[3]()    -- output: Hello World!

Tables can contain actual functions

local myTable = {
    100,
    100,
    function() print( "Hello World!" ); end,
    true
}

myTable[3]    -- output: Hello World!

LUA Tables as Arrays

First of all some points about LUA tables:

  • You do not need to declare their size.
  • They can be used like arrays.
  • They also fill the role of dictionaries (associative arrays)
  • You can have mixed data types in tables, similar to structures in other languages.

Tables can be used like arrays

local shapes1 = {"circle", "square", "triangle", "hexagon" }

local shapes2 = {}
shapes2[1] = "circle"
shapes2[2] = "square"
shapes2[3] = "triangle"
shapes2[4] = "hexagon"

local shapes3 = {
[1] = "circle",
[2] = "square",
[3] = "triangle",
[4] = "hexagon"
}

Elements in each of the above table declarations can be accessed by numerical index just like any other array. The following will all have the same output.

print(shapes1[2] )
print(shapes2[2] ) -- square
print(shapes3[2] ) -- square

Built in functions

In LUA there is a namespace which contains functionality for adding elements, removing elements and sorting an array.

Adding Elements

table.insert(table_name, [pos, ] value)

The position parameter is optional, if omitted the element is added at the end as the last element.

Removing Elements

value = table.remove(table_name [, pos] )

The position parameter is optional, if ommitted the last element in the array will be removed.
The remove function returns removed element.

Sorting Elements

table.sort(table_name [, comp])

By default an alpha numeric sort is performed on the element value.

Getting the length of the array
To get the length of the array simply prepend # to the name of the array

#shapes1

Iterating Arrays
Iterating an array can be done with a simple for loop. The structure of a for..loop is

for variablename = start_value, end_value, step_value do
...
end

You can also use the iparis function which returns an iterator function to iterate the array.

for loopCounter = 1, #shapes1 do
  print(shapes1[loopCounter])
end

for index, value in ipairs(shapes1) do
  print(index, value)
end

output of the above is

circle
square
triangle
hexagon

1 circle
2 square
3 triangle
4 hexagon