List-Styled Tables
What is a List?
Value-indexed tables are a commonly-neglected technique in Lua:
We'll call them Lists, for short. They're incredibly useful! The idea is that by using the actual value we intend to store in the table as the key itself, we can retrieve and remove it from the table much easier and faster. This has many uses:
- Good for when the order of data saved is unimportant and each value is unique within the table.
- Creating a table in which the same value can't exist more than once.
- Rapidly checking whether a value is stored in a table without the use of iteration (seen above).
Two-Dimensional Lists
We can take it one step further with 2D Lists:
Look at how much faster and simpler that is! If you wanted to, you could even replace true
in the list with some kind of data. This makes the list into a dictionary.
So remember: Any time you need to save distinct data in an unordered table, opt to use a List instead!