Keys and Values

In the lesson on lists, a table kept its items in order, under the numbers 1, 2, 3… That works for a shopping list. But Emma also wants to write down what's in her barn: 12 carrots, 30 potatoes and 4 pumpkins. A list {12, 30, 4} would hold the numbers, but nothing in it would say which number belongs to which crop.

A table can do more than a list. Instead of the numbers 1, 2, 3, it can use names:

-- Emma's barn
local barn = {carrot = 12, potato = 30, pumpkin = 4}

print(barn.carrot)
print(barn["potato"])

This prints:

12
30

A table used like this is called a dictionary. Just as you look up a word in a real dictionary to find what it means, you look up a key (carrot) to find its value (12). Inside the curly braces are key–value pairs, separated by commas: the key, = and the value. A value can be anything: a number, a text, true or false, even another table.

Lua has no special type for dictionaries. It's the same table you know from lists, and type(barn) is "table". A list is simply a table whose keys are 1, 2, 3…

There are two ways to read a value, and they mean exactly the same:

  • barn.carrot: a dot and the name of the key;
  • barn["carrot"]: the key as a text in square brackets, like an index in a list.

The dot is shorter, so you'll see it more often. But it works only for keys that follow the rules for variable names: letters, digits and _, not starting with a digit, and not a keyword such as end. A key with a space, such as sweet corn, must be written in square brackets and quotes, both in the curly braces and when you read it:

local barn = {carrot = 12, ["sweet corn"] = 9}

print(barn["sweet corn"])

This prints:

9

Without the brackets, Lua reads only sweet and doesn't know what to do with corn:{sweet corn = 9} ends with main.lua:1: '}' expected near 'corn'.

A table with more pairs is easier to read with each pair on its own line. A comma after the last pair is allowed, which is handy when you add another line later:

local carrot = {
    name = "Carrot",
    days = 3,
    sellPrice = 5,
}

print(carrot.name .. " sells for " .. carrot.sellPrice .. " coins")

This prints:

Carrot sells for 5 coins

To see a whole dictionary, print(barn) won't help: as with lists, it prints only table: 0x…. And table.concat joins only the items of a list. You'll learn to print every pair later in this lesson.

Typical mistakes:

  • A missing comma between two pairs: {carrot = 12 potato = 30} ends with main.lua:1: '}' expected near 'potato'.
  • A colon instead of =, as some other languages write it: {carrot: 12} ends with main.lua:1: <name> expected near '12'.
  • A key in quotes, but without the square brackets: {"carrot" = 12} ends with main.lua:1: '}' expected near '='.

Instructions

Print how many carrots and potatoes are in the barn. Read the numbers from the table: once with a dot, once with square brackets. The program prints:

Carrots: 12
Potatoes: 30

Emma has harvested 9 ears of sweet corn. In the curly braces, add a pair with the key "sweet corn" and the value 9. The key has a space in it, so it needs square brackets.

At the end of the program, print the sweet corn and the total of all three crops. Read every number from the table:

Sweet corn: 9
Total: 51

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/10