Tables with non-numeric keys (or indexes)

Up till now we used tables as classical arrays, i.e. ordered lists where every element is accessed by its number.

Tables allow textual indexing also, such structures are called "hash-tables" or "dictionaries" (and this double-use of as array and hashtable happens in some other languages, notably PHP and JavaScript).

When this is convenient? Suppose for simplicity we create a "knowledge base" where user enters last name of one of US presidents and gets detailed information of the given historical person:

pres = {}         -- let create it empty and fill one by one
pres['washington'] = 'George Washington, 1732-1799, 1-st'
pres['lincoln'] = 'Abraham Lincoln, 1809-1865, 16-th'
pres['roosevelt'] = 'Theodore Roosevelt, 1858-1919, 26-th'

lastname = io.read('*l')
print(pres[lastname:lower()])      -- note the trick with converting to lowercase!

Try it - it works. If the name is not found, you'll see nil - this could be processed separately if necessary, i.e. asking user to retype etc.

Important property of the underlying "hash-table" mechanism is that when we need to look-up value by some key, machine doesn't need to iterate over all values comparing them one by one. It finds and returns necessary key in a small and almost constant time, not depending on the total number of elements. This may be similar to how "index" in the database works, but we'll learn about this somewhere later.

As a sidenote, there is special syntax to fill such table with data immediately on creation (rather than creating it empty), but this is mainly convenient for small data:

pres = { washington = 'George Washington, 1732-1799, 1-st', lincoln = 'Abraham Lincoln, 1809-1865, 16-th',
    roosevelt = 'Theodore Roosevelt, 1858-1919, 26-th'}

Also in this case keys are specified without quotes which makes it difficult if any special characters are involved.

For Exercise firstly play a bit with the code. What if we don't use lowercase conversion (and enter Lincoln for example)? What is reported as the size of the table, e.g. if we print out #pres? Secondly, try the problem Matching Words which is very easily solved using such type of data storage.