Earlier I mentioned that tables contain two types of arrays.
The indexed array stores its elements in contiguous memory.
The second type is called a ‘hash table.’
Rather than storing its elements in contiguous memory and indexed with integer indices, hash tables index elements via ‘keys‘.
These keys are used to compute the location where the element is stored. A bit like storing books in alphabetical order on a bookcase.
This is called a ‘key-value pair‘.
(I’m using the term “stored” loosely here. Something like a part requires much more space than say, a number, meaning the part is unlikely to actually fit inside an array element. In this instance, what’s actually stored is the memory address to the part. But now we are getting way beyond the scope of this series. I digress.)
The hash table allows you to use non-integer keys to index an element. For instance, if you wanted to use a string as a key:
local someList = {}
someList["someKey"] = "Hello world!"
someList["second"] = 3.14
someList["forty two!"] = math.random(1000)
someList["42"] = math.random(-1000, 1000)
print(someList)
And just like you saw previously, the array can be initialized when it is constructed. (Separated onto multiple lines for clarity)
local someList = {
["someKey"] = "Hello world!",
["second"] = 3.14,
["forty two!"] = math.random(1000),
["42"] = math.random(-1000, 1000)
}
print(someList)
If a string-based key obeys variable naming rules, the dot operator may be used to access an element.
local someList = {
["someKey"] = "Hello world!",
["forty two!"] = math.random(1000),
["_1234"] = math.random(1000),
["_pi_"] = 3.14
}
print(someList.someKey, someList._pi_, someList["forty two!"], someList._1234)
One of the neat things about key-value pairs is that you’re not limited to just string-based keys. This uses a part as the key to index another part.
local keyPart = Instance.new("Part")
local valuePart = Instance.new("Part")
keyPart.Parent = game.Workspace
valuePart.Parent = game.Workspace
local someList = {
["forty two!"] = "Hello world!",
[keyPart] = valuePart
}
print(someList)
--[[ Output: {
["forty two!"] = "Hello world!",
[Instance(1F1088F04D8)] = Part
}]]
Mixed Tables
Lua automatically stores integer indexed elements in the standard array part and key indexed elements in the hash table. This creates a ‘mixed array‘ of key-value pairs and indexed elements.
The number of elements in the indexed array can be counted if there is an element at index 1. Use the # operator to get this count.
local someList = {}
print(#someList) --Output: 0
someList[1] = math.random()
someList[2] = math.random()
someList[3] = math.random()
someList[4] = math.random()
print(#someList) --Output: 4
The count ends if it reaches a hole. Negative and zero indices are not counted. And key-value pairs cannot be counted.
local someList = {}
someList["pi"] = 3.14
someList[-1] = math.random()
someList[0] = math.random()
someList[1] = math.random()
someList["randomNumber"] = math.random()
someList[2] = math.random()
someList[3] = math.random()
someList[5] = math.random()
print(#someList) --Output: 3
Although this table contains a total of 8 elements, only indices 1, 2, and 3 are counted as part of the array.
So at this point you might be confused as to how a table is different from an array. And how an array is different from a hash-table. But yet ‘array‘ is often used to refer to both.
The short answer is don’t stress about that either.
‘Table‘ and ‘array‘ are used interchangeably in Lua simply because tables are used so often as arrays.
Lua hides away a lot of the complexity so ‘array‘ is broadly used to refer to any table that stores a collection of elements.
When it comes down to it, this is just part of computer science jargon. As with jargon for any industry, it becomes familiar repetition. But the important thing isn’t the jargon.
The important thing is the repetition. The important thing is the repetition of mundane and seemingly inconsequential fundamentals across a broad range of computing challenges.
This series is designed to give you the skills to take on those challenges.