Creating Lists
In the lesson on loops, you learned to repeat code. In this lesson, you'll learn to keep many values under one name.
Emma grows several crops on Sunflower Farm. With what you know so far, you would need a variable for each of them:
local crop1 = "carrot"
local crop2 = "potato"
local crop3 = "pumpkin"That's a lot of typing, and a loop can't go through them: crop1 and crop2 are just two names that have nothing to do with each other. Lua has a better tool: a table. A table that holds values in order, one after another, is called a list. You write its values in curly braces, separated by commas:
local crops = {"carrot", "potato", "pumpkin"}The values in a list are its items (also called elements). This list has three items, in a fixed order: "carrot" is the first one, "pumpkin" the last one. Tables are the only way Lua has to group values together. In this lesson, they hold lists. In later lessons, the same tables will hold dictionaries, and even objects.
Each item has a position, called its index. In Lua, indexes start at 1: the first item has index 1, the second index 2, and so on. To read an item, write the list's name and the index in square brackets:
local crops = {"carrot", "potato", "pumpkin"}
print(crops[1])
print(crops[3])This prints:
carrot
pumpkinThis is how the items and their indexes line up:
crops: | "carrot" | "potato" | "pumpkin" |
index: 1 2 3Most other programming languages (Python, JavaScript, C…) count indexes from 0. Lua counts from 1, as people do when they count things. If you learn another language one day, watch out for this difference.
What happens with an index where there's no item, such as 0 or 4? Many languages stop the program with an error. Lua doesn't: you just get nil, the value that means “nothing”:
local crops = {"carrot", "potato", "pumpkin"}
print(crops[0], crops[4])This prints:
nil nilThe nil causes trouble only later, when you use it. Joining it to a text, for example, ends with an error:
local crops = {"carrot", "potato", "pumpkin"}
print("Crop: " .. crops[4])main.lua:2: attempt to concatenate a nil value (field 'integer index')The message says that you tried to join (concatenate) a nil, and that it came from a table: field is Lua's word for a place in a table, and this one had a whole number as its index. When you see such an error, check the index.
Items can be numbers too, and a list can also be empty: {}. You'll learn to fill an empty list later in this lesson. An item you read from a list is an ordinary value, so you can calculate with it or join it to a text:
local eggs = {4, 6, 5}
print(eggs[1] + eggs[2])
print("Eggs today: " .. eggs[3])This prints:
10
Eggs today: 5Instructions
The program should print Emma's first crop, but when you run it, it ends with an error: main.lua:3: attempt to concatenate a nil value (field 'integer index'). Fix the index so that the program prints First crop: carrot.
Below it, print the last crop, the fourth one, the same way: Last crop: strawberry.
Below that, create a local variable eggs with a list of the eggs Emma's hens laid in the last three days: 4, 6 and 5, in this order.
Print how many eggs the hens laid in the three days together. Add up the three items of eggs in the code; don't calculate the number yourself: Eggs in total: 15.
Start programming for free
1/11