How to use tables
(and use them well)
Welcome to this tutorial about using tables. This tutorial is split into parts:
Part I - Creating a table
A table is a list of values, which are all stored under one variable.
In Lua, a table is an associative array, which means that any values stored in one are referenced by a key, which can be a number, string, boolean or even another table!
To make a table, simply do this:
The variable myTable is now an empty table.
A table is a list of values, which are all stored under one variable.
In Lua, a table is an associative array, which means that any values stored in one are referenced by a key, which can be a number, string, boolean or even another table!
To make a table, simply do this:
local myTable = {}
The variable myTable is now an empty table.
Part II - Numbers as keys
If you already have a table, you can insert a value referenced by a number into the table with one of these methods:
You can then refer to this value as:
You can replace 1 with any number you like, and it will still work. Note that myTable:insert() will shift the positions of the other values in a table, whereas referencing the table directly (myTable[1] = "...") will overwrite the value at position 1.
If you already have a table, you can insert a value referenced by a number into the table with one of these methods:
-- This... myTable[1] = "This is item 1 of the table" -- ... does the same as this myTable:insert(1, "This is item 1 of the table")
You can then refer to this value as:
print(myTable[1]) -- Prints out "This is item 1 of the table"
You can replace 1 with any number you like, and it will still work. Note that myTable:insert() will shift the positions of the other values in a table, whereas referencing the table directly (myTable[1] = "...") will overwrite the value at position 1.
Part III - Strings as keys
If you are creating an array with no specific order (e.g. a username and password table) you can also use strings as a key:
You can't use myTable:insert() with strings (I think). To reference a value, you can just do:
If you are creating an array with no specific order (e.g. a username and password table) you can also use strings as a key:
myTable["ZudoHackz"] = "MyPassword" myTable["SomeoneElse"] = "SomePassword"
You can't use myTable:insert() with strings (I think). To reference a value, you can just do:
print(myTable["ZudoHackz"]) -- Or print(myTable.ZudoHackz)
Part IV - Initializing a table with data already in it
This works, but it is quicker and shorter to do this:
If you create a table like this, myTable[1] would be set to "blah1", myTable[2] would be "blah2" etc.
You can also do this:
If you do it this way, you can change the order easily:
Or even do this:
This also works with anything else as a key:
local myTable = {}
myTable[1] = "blah1"
myTable[2] = "blah2"
myTable[3] = "blah3"
...
This works, but it is quicker and shorter to do this:
local myTable = {"blah1","blah2","blah3"}
If you create a table like this, myTable[1] would be set to "blah1", myTable[2] would be "blah2" etc.
You can also do this:
myTable = {[1] = "blah1", [2] = "blah2", [3] = "blah3"}
If you do it this way, you can change the order easily:
local myTable = {[3] = "blah1", [2] = "blah2", [1] = "blah3"}
Or even do this:
local myTable = {[1] = "blah1", [6] = "blah2", [300] = "blah3"}
This also works with anything else as a key:
local myTable = {["one"] = "blah1", ["two"] = "blah2", ["three"] = "blah3"}
Part V - Using tables
Tables are used for storing similar information. But, they can also be used for other things, like:
This isn't really a use of the table, but it is very useful to learn about
Tables are used for storing similar information. But, they can also be used for other things, like:
Capturing return values
Some functions, like os.pullEvent() return different amounts of return values. You could do something like this:
but you still can't be sure you have saved all the return values. However, a setup like this:
would definitely capture all of the values, and definitely return them all. More on the for loop later. Anyway, if you put the function call in curly { } brackets, it will return a table. myEventData[2] in the second example is equal to b in the first example.
Some functions, like os.pullEvent() return different amounts of return values. You could do something like this:
local a, b, c, d, e = os.pullEvent() print(a) print(B)/>/>/> print(c) print(d) print(e)
but you still can't be sure you have saved all the return values. However, a setup like this:
local myEventData = {os.pullEvent()}
for _, value in pairs(myEventData) do
print(v)
end
would definitely capture all of the values, and definitely return them all. More on the for loop later. Anyway, if you put the function call in curly { } brackets, it will return a table. myEventData[2] in the second example is equal to b in the first example.
Please read
IP.Board screwed up the third line of the first example in this section. It is print b but with brackets, rather than spaces.
IP.Board screwed up the third line of the first example in this section. It is print b but with brackets, rather than spaces.
This isn't really a use of the table, but it is very useful to learn about
Iterating through a table
With the pairs() function, you can iterate through a table. Here is an example:
This piece of code repeats 3 times (there are three values in the table) and prints:
So this can be very useful. How about this?
So iterating through tables is very, very useful.
With the pairs() function, you can iterate through a table. Here is an example:
local myTable = {"blah1","blah2","blah3"}
for key, value in pairs(myTable) do
print("The value of the key " .. key .. " is ".. value)
end
This piece of code repeats 3 times (there are three values in the table) and prints:
The value of the key 1 is blah1 The value of the key 2 is blah2 The value of the key 3 is blah3
So this can be very useful. How about this?
local myTable = {"blah1","blah2","blah3"}
local function isInTable(table, value)
for _, v in pairs(table) do -- Use _ if you don't need that return value.
if v == value then
return true, k
end
end
return false
end
isInTable(myTable, "blah1") -- Returns [true, 1]
isInTable(myTable, "blah2") -- Returns [true, 2]
isInTable(myTable, "blah3") -- Returns [true, 3]
isInTable(myTable, "blah4") -- Returns false
So iterating through tables is very, very useful.
Thank you for reading this tutorial! Please point out any mistakes, and advise me on using tables even better!
-Z












