What's the difference between a table and a dictionary?

Tables

Tables are both Arrays and Dictionaries, they can hold multiple datatypes and are really useful.
They have an “index” and a “value”, when you do something like: table[index] it’ll give you the value of that index.

Arrays

Arrays have ordered indexes starting from 1 to how ever many you give, in arrays you do not give the index - instead it gives the index itself.
An array will look like:

local Array = {3,2,1}

Loop through this and you’ll see how they’re ordered:

for index, value in pairs(Array) do
    print(string.format("%d | %d", index, value)) 
end
--[[
1 | 3
2 | 2
3 | 1
]]

Dictionaries

In Dictionaries you give the index, they’re not ordered.
A Dictionary will look like:

local Dictionary = {
    Index1 = 3;
    Index2 = 2;
    Index3 = 1;
}

Loop through this and you’ll see the specified indexes:

for index, value in pairs(Dictionary) do
    print(string.format("%s | %d", index, value))
end
--[[
Index1 | 3
Index2 | 2
Index3 | 1
]]

Dictionaries can also have spaces and non-numeric/non-alphabetic characters in their name though I don’t recommend it, here’s how:

local Dictionary = {
    ["Bro do"] = true;
    ["you even"] = true;
    ["lift?"] = true;
}
25 Likes