Back

Activity 1.18: Tables as Lists

divider

Activity 1.18

Tables as Lists

Key Concepts

Many Values, One Name

Counting From One

The Exam Agrees

What You Are Building

An 800 by 600 dark window with three light blue filled circles in a horizontal row, evenly spaced across the middle

Three circles, and not three variables.

This Does Not Scale

local x1 = 150
local x2 = 400
local x3 = 650

Three circles, three variables. Thirty circles?

A Table

local xs = {150, 400, 650}

One name. Three values. The braces are what make it a table.

Counting From One

local crew = {"Ada", "Bo", "Cy"}
print(crew[1])
print(crew[2])
print(crew[3])

Counting From One

local crew = {"Ada", "Bo", "Cy"}
print(crew[1])
print(crew[2])
print(crew[3])
Terminal window
Ada
Bo
Cy

So What Is at Zero?

local crew = {"Ada", "Bo", "Cy"}
print(crew[0])

So What Is at Zero?

local crew = {"Ada", "Bo", "Cy"}
print(crew[0])
Terminal window
nil

Nothing is there. The list starts at one.

Changing an Element

local xs = {150, 400, 650}
xs[2] = 500
print(xs[2])

Changing an Element

local xs = {150, 400, 650}
xs[2] = 500
print(xs[2])
Terminal window
500

One slot changed. The other two are untouched.

The Exam Agrees

The exam's notation counts from one as well.

xs[1] is the first element in both.

Most languages count from zero. This is the one place where what you type and what the exam prints are the same thing.

Today's Objectives

  • Make a table that holds several values
  • Read one element out of it by index
  • Change one element without touching the others
  • Say why the first element is at index 1

Key Terms

Table
One name holding many values.
Element
One of the values stored in a table.
Index
The number that picks out one element.
One-indexed
Counting starts at 1, the way the exam reference sheet does it.

'F' → Fullscreen

divider

Build

This activity draws, so it needs a window. Copy template-game, rename the copy to 1-18-tables, and work in its main.lua.

Task 1: Three Circles, One Table

Type this and run it. You should get the picture at the top of the page.

main.lua
local xs = {150, 400, 650}
function love.draw()
love.graphics.setBackgroundColor(0.11, 0.11, 0.15)
love.graphics.setColor(0.28, 0.72, 0.95)
love.graphics.circle("fill", xs[1], 300, 40)
love.graphics.circle("fill", xs[2], 300, 40)
love.graphics.circle("fill", xs[3], 300, 40)
end

Read the three circle lines out loud before you run it. Each one asks xs for a different position, and the only thing that changes between them is the number in the brackets.

Task 2: Move the Middle One

Add one line, above love.draw:

main.lua
local xs = {150, 400, 650}
xs[2] = 500
function love.draw()
love.graphics.setBackgroundColor(0.11, 0.11, 0.15)
love.graphics.setColor(0.28, 0.72, 0.95)
love.graphics.circle("fill", xs[1], 300, 40)
love.graphics.circle("fill", xs[2], 300, 40)
love.graphics.circle("fill", xs[3], 300, 40)
end

Run it. The middle circle moves and the other two do not. You did not touch the lines that draw them, and you did not touch the other two values — you changed one slot in the table, and the circle that reads that slot moved with it.

Task 3: Look at Index Zero

Predict first, on your sheet. Then add these two lines to the top of your file and run it.

main.lua
local xs = {150, 400, 650}
print("xs[1] is " .. tostring(xs[1]))
print("xs[0] is " .. tostring(xs[0]))
Terminal
xs[1] is 150
xs[0] is nil

nil is Lua's word for nothing is here. There is no element at index 0, because the first one is at index 1.

Notice what did not happen: the program did not crash. It printed nil and carried straight on. Hold that thought — it is the whole subject of the next activity, and it is more dangerous than it looks.

Challenge (Optional): A Fourth Circle

Add a fourth position to the table and a fourth circle to the screen. Count how many separate places in the file you had to edit to do it.

Write that number on your sheet. Two activities from now you will do the same thing by editing exactly one place.

divider

Same Idea, Exam Notation

Read this panel twice, because it is good news and you will not get much of it.

Lua
local xs = {150, 400, 650}
local first = xs[1]
xs[2] = 500
print(xs[1])
Exam Notation
xs ← [150, 400, 650]
first ← xs[1]
xs[2] ← 500
DISPLAY(xs[1])

The indexes are identical

xs[1] is the first element on the left and the first element on the right. Not the second. Not off by one. The same.

LuaExam notation
First element of xsxs[1]xs[1]
Change the second elementxs[2] = 500xs[2] ← 500

What is different is only how it is written

  • The exam writes assignment as , not =.
  • The exam builds a list with square brackets, and Lua builds a table with braces.
  • The exam displays a value with DISPLAY(xs[1]), and Lua prints it with print(xs[1]).

None of those change an answer. They are spelling. The index is the part that changes answers, and the index agrees.

One real difference is coming, and it is not this one. On the exam, assigning one list to another makes a copy. In Lua it does not. That is the next activity, and it is the sharpest question in the quarter.
divider

Checkpoint

  • Three circles on screen, matching the picture at the top
  • The middle circle moved by changing one element, not by editing a circle line
  • xs[0] printed and its result written on your sheet
  • Your prediction written down before you ran Task 3
divider

Reflection

Answer the following questions before submitting your work.

  1. A table with three values has a last index of 3. Say what the last index of a table with a hundred values is, and explain the rule in a sentence.
  2. You asked for xs[0] and got nil instead of an error message. What does that tell you about where the list actually begins?
  3. Someone who learned JavaScript or Python first would expect xs[0] to be the first element. Describe a question on the exam they could get wrong because of that habit.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete