I’ve found the official Lua reference very helpful, and readable if you have some experience w/ coding. You can do a lot with normal tables.
Excerpted from the reference for Lua 5.3 (Lua 5.3 Reference Manual):
There are eight basic types in Lua: nil , boolean , number , string , function , userdata , thread , and table . […]
The type table implements associative arrays, that is, arrays that can have as indices not only numbers, but any Lua value except nil and NaN. […] Tables can be heterogeneous ; that is, they can contain values of all types (except nil ).
Tables are the sole data-structuring mechanism in Lua; they can be used to represent ordinary arrays, lists, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing
a.name as syntactic sugar for
Like indices, the values of table fields can be of any type. In particular, because functions are first-class values, table fields can contain functions.
What does all this mean?
A table is a basic data type, like a number or a string. So anywhere you’d store a number or pass a string as an arg, you can reference a table instead.
A table implements an associative array. What’s an associative array? Imagine a teacher’s paper grade book with each student’s name written in pencil in a list, accompanied by their individual final grade. Finally, let’s say this teacher is compulsively thorough and will never write down a name w/o a grade beside it or vice versa.
The teacher can check a specific student’s grade, add a name and associated grade to the list, erase a name/grade, rearrange the order, change a specific grade, copy the list to another page, etc.
This could be seen as an associative array, that is: each name (“key”) is associated with a grade (“value”). But since the teacher is working with pencil and paper, nothing is stopping them from writing a number in place of a name. Or an obscene word in place of a grade. Or a letter grade in place of a name.
If they were perverse, extremely bored, and wrote really tiny, they could even fit another whole name/grade list into the space for one of the names. Or fit all that into the space for a single grade. And then erase it all.
Essentially, they could write anything that can be put to paper with pencil as either a key or the value associated with that key. In general, this is like a table in Lua.
So then, what kind of things can you with tables? All the kinds of things the teacher can do with the list of names/grades.
Within what constraints? The teacher is limited by what can be written on paper with a pencil. Lua limits us to using its basic data types for keys and values (excepting ‘nil’ and NaN). So you can use numbers, strings, true/false, other tables, and a few more things as either keys or values. Lua also gives us an enormous sheet of blank paper to start with, so we’re unlikely to run out of space on it.
Now that we have a firm grasp on 1) what a table is and 2) what we can do with it, how exactly can we work with tables in code?
To be continued…
<foreboding music wafts through gaps in the castle’s stone walls and a malevolant chuckle echoes off the vaulted ceilings, quickly yielding to a convulsive fit of coughing…>