Back

Activity 1.20: Going Through a Table

divider

Activity 1.20

Going Through a Table

Key Concepts

A Loop and a Table Fit Together

Two Ways to Walk a List

N Things From N Values

What You Are Building

Five blue circles scattered at different heights across a black window

Five circles in five places, from two tables and one loop.

You Have Both Halves Already

print(crew[1])
print(crew[2])
print(crew[3])

You Have Both Halves Already

print(crew[1])
print(crew[2])
print(crew[3])

A table holds many values. A loop counts. The index is the count.

Which Is One Line

for i = 1, #crew do
print(crew[i])
end

Which Is One Line

for i = 1, #crew do
print(crew[i])
end

#crew from last session is the end of the count, so the loop fits the table however long it is.

Add a fourth name and nothing else changes.

Traversal

Visiting every element of a list, in order. The exam's word, and it appears constantly.

Almost everything anyone does with a list is a traversal with one line changed in the middle.

Lua Has a Shorter Way

for i, name in ipairs(crew) do
print(i, name)
end

Lua Has a Shorter Way

for i, name in ipairs(crew) do
print(i, name)
end
Terminal window
1 Ada
2 Bo
3 Cy

Two Names, Not One

for i, name in ipairs(crew) do
i
Which one you are on. 1, then 2, then 3.
name
The value itself. You do not have to look it up.

Same idea as the mouse giving you two answers at session 13.

Which One Should You Use?

for i = 1, #t
When you need the index for something else — a second table, a position, a count.
ipairs
When you only want the values.

ipairs gives you both, so it is rarely wrong. The indexed one is what the exam looks like.

Two Tables, One Index

local xs = {120, 260, 400, 540, 680}
local ys = {180, 320, 240, 430, 200}

xs[3] and ys[3] are the same circle. The index is what connects them.

Parallel lists. Nothing in the code says they belong together — you have to keep them the same length yourself.

Today's Objectives

  • Traverse a table with an indexed loop
  • Traverse a table with ipairs
  • Use one index across two tables
  • Draw N shapes from N stored values

Key Terms

Traversal
Visiting every element of a list, in order.
ipairs
A loop that hands you the index and the value together.
Parallel lists
Two lists where the same index means the same thing in both.

'F' → Fullscreen

divider

Build

Task 1: Both Ways

Copy template-console to 1-20-walk. Make a table of five things — anything — and print it twice, once each way:

main.lua
local crew = {"Ada", "Bo", "Cy", "Dee", "Eli"}
for i = 1, #crew do
print(i .. ": " .. crew[i])
end
print("---")
for i, name in ipairs(crew) do
print(i .. ": " .. name)
end

The output is identical. Then add a sixth name and run it again without touching either loop.

Task 2: Do Something to Each One

Make a table of five numbers. In one traversal, print each number doubled. In another, add them all up and print the total:

main.lua
local scores = {12, 8, 21, 3, 17}
local total = 0
for i, score in ipairs(scores) do
total = total + score
end
print("Total: " .. total)
print("Count: " .. #scores)
print("Average: " .. total / #scores)
local total = 0 goes above the loop, not inside it. This is the same rule as session 15's tries and session 14's hits. Third time; it should be a habit by now.

Task 3: Five Circles From Two Tables

Copy template-game to 1-20-sprites:

main.lua
local xs = {120, 260, 400, 540, 680}
local ys = {180, 320, 240, 430, 200}
function love.draw()
for i, x in ipairs(xs) do
love.graphics.setColor(0.3, 0.6 + i / 20, 1)
love.graphics.circle("fill", x, ys[i], 34)
end
end
Five circles at the positions stored in the two tables

Then add a sixth position to both tables and run it. A sixth circle appears and you changed no code.

Task 4: Break the Pair on Purpose

Add a value to xs and not to ys. Run it.

The window turns blue. Copy the message and work out which table the loop was walking, and which one ran out.

Then do the opposite — add to ys and not xs. This one does not crash, and that is worse. Write down what you see and why.

Task 5: Make Them Yours

Add a third parallel table and use it for something — radius, color, or both. Then pick one:

  • Make one circle in the list move, using its index to pick which
  • Draw a line from each circle to the next one
  • Use the index to number the circles on screen

Challenge (Optional): The Hole

Back in the terminal:

main.lua
local gappy = {"a", nil, "c"}
for i, v in ipairs(gappy) do
print(i, v)
end
print("#gappy is " .. #gappy)
Terminal
1 a
#gappy is 1

The "c" is in the table and the traversal never reached it. So did #gappy.

This is last session's warning with a loop attached: a table with a gap in it is not a list, and every tool that walks a list stops at the hole. Write down what this means for a table you build by deleting things out of the middle.

divider

Same Idea, Exam Notation

The exam traverses lists constantly, and it does it with the loop that has no counter — so a counter gets built by hand.

Lua
local crew = {"Ada", "Bo", "Cy"}
for i = 1, #crew do
print(crew[i])
end
Exam Notation
crew ← ["Ada", "Bo", "Cy"]
i ← 1
REPEAT LENGTH(crew) TIMES
{
DISPLAY(crew[i])
i ← i + 1
}
LuaExam notation
How many elements#crewLENGTH(crew)
The first onecrew[1]crew[1]
Walk the whole listfor i = 1, #crew doREPEAT LENGTH(crew) TIMES
There is no ipairs on the exam. It is a Lua convenience, and a good one. Use it in your own programs and expect the indexed form on the exam.

The index still starts at 1 in both, which is the agreement from session 18 doing real work. A traversal written in Lua and the same traversal in exam notation visit the same elements in the same order.

divider

Checkpoint

  • 1-20-walk prints the same list two different ways
  • Adding a sixth item changed no loop
  • 1-20-sprites draws its circles from two parallel tables
  • Both mismatch experiments from task 4 are written down
  • A third parallel table is doing something
divider

Reflection

Answer the following questions before submitting your work.

  1. You added a sixth circle by adding two numbers and changing no code. Explain what makes that possible, and describe what you would have had to do in session 2 to add a sixth shape.
  2. A longer xs crashed and a longer ys did not. Explain the difference, and say which of the two you would rather have happen in a program you were about to hand in.
  3. Nothing in the code says xs and ys belong together. Explain what actually connects them, and describe one way that could go wrong that Lua would never warn you about.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete