Main Content

Lua tables - they are everything

Archive - Originally posted on "The Horse's Mouth" - 2010-03-30 23:12:55 - Graham Ellis

In PHP, it sometimes feels like the answer to every question is "there's a function to do that" and in Lua, it seems the answer is always "it's a table" or "use a table"!

I'm running a Lua course this week, and I have just posted up an example in which I set up a table and showed some of the flexibilities - full example [here].

A table is Lua's collection variable - a variable that holds other variables. It may be indexed by numbers (and that's an ordered table, which can be sorted), or by member name (an unordered table, using a hashing technique, so unsortable directly), or even as a mixture, as I've done in the example just quoted.

Variables themselves are very flexible - Lua is a tiny language, and so variables also contain just about everything in order to cut down on the footprint ... that means that a variable can contain a number, a string of text, or another table or a named block of code (yes, that's a function) - all are in the same "namespace" and all are handled by the same base language code.

Do you need to see under Lua'a cover like this in order to use it? Well - it certainly helps to understand just a smidgin of what's going on inside, as that allows you to make the very best use of the language, and allows you to quickly find and rectify coding issues that are associated with this sharing of namespace.