Day 2 begins: Lua Tables

A quick improvement. I complained about the code necessary to load a file into the REPL. The assert(loadfile('file.lua'))() seemed a bit long. There is a better way:


That’s more like it!

In writing this, I’ve come across numerous places where I’d like to add even more examples that I’ve created to try things out. If you ever want to try something for yourself, you don’t need to go to the trouble to install Lua (or, since you’re better than I am, build it for yourself). There’s an on-line interpreter with some demos at .

So, do you find that having one data structure that is indexed by integers and another that’s indexed by field names confusing? Would you prefer to be able to do:

a[3] = 4 = 'Numbers'

Yep, Lua lets you! This is the table data structure that makes Lua worth learning.  Or so says Tate.

Table Basics

So, you can use tables for named fields as in:

stateFL = {
   capital = 'Tallahassee',
   bird = 'mockingbird',
   population = 19890000
stateFL.nickname = 'Sunshine State'

A quick function to step through all the entries is:

function print_table(t)
    for k, v in pairs(t) do
        print (k .. ": " .. v)

And when printed you get:

population: 19890000
bird: mockingbird
capital: Tallahassee
nickname: Sunshine State

Okay, that looks like a dictionary like thing in many scripting languages. But then the fun begins.

You can also create an array with similar syntax:

citiesFL = {

citiesFL[5] = 'North Palm Beach'

And as output, get:

1: Gainesville
2: Jacksonville
3: Miami
4: Tampa
5: North Palm Beach

There is something a little surprising to those of us who have been in CS for way too long. The list begins at position 1, not position 0. Remember when we used to start counting with 1? Yeah, me neither.

Things get to be fun when you combine these, as in (using the previous state table):

stateFL[1] = 'Gainesville'
stateFL[2] = 'Jacksonville'
stateFL[3] = 'Miami'
stateFL[4] = 'Tampa'
stateFL[53] = 'Newberry'

Of course, there’s no need to have the numbers in consecutive order. You could even get crazy and add:

stateFL[-12] = 'Key West'

and if you really need to have an element at position 0, sure, go ahead:

stateFL[0] = 'Alachua'

It’s not like the data is stored in separate areas. In fact, the numbered data is mixed with the named data:

1: Gainesville
2: Jacksonville
3: Miami
4: Tampa
population: 19890000
-12: Key West
0: Alachua
53: Newberry
bird: mockingbird
capital: Tallahassee
nickname: Sunshine State

If you’re thinking “Well, no big deal, the number is just converted to a string,” nope. Florida is getting full, so let’s move north a bit:

citiesGA = {}
citiesGA[1] = 'Macon'
citiesGA[4] = 'Savannah'
citiesGA['1'] = 'Atlanta'
citiesGA['01'] = 'Richmond Hill

Printing this gives:

1: Macon
1: Atlanta
4: Savannah
01: Richmond Hill

A little more fun with the basics. You can access the named fields using the array notation as well, as in citiesFL["capital"] . And, just as with traditional arrays and dictionaries, this doesn’t need to be a string, so

item = 'capital'
print (citiesFL[item])

works just fine. And you do know that citiesFL.item is something completely different, right?

Still, this is not worth a whole new language. Not yet. Lua tables allow you to customize how the table works. Want to create a sparse matrix and have a different value returned? That’s where metatables come into play, but this is long enough for now, so that’ll have to wait until tomorrow.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: