← Back to course
review ✦ 50 Studs

Arrays Recap

Lists in Lua: make one, read by index, add to the end, and count it.

An array is just a numbered list in a single box. Instead of making fruit1, fruit2, fruit3, you make one table and let the numbers do the organizing. You’ve built these already, so let’s nail down the two details people always trip on.

local team = {"red", "blue", "green"}
print(team[1])   -- red
print(#team)     -- 3

First detail: Lua counts from 1, not 0. If you’ve seen other languages, this feels weird at first, but it’s actually how normal people count, so lean into it. The first item is team[1].

Second detail: #team gives you the length, the count of items. That little # is your best friend for loops, because for i = 1, #team do walks the whole list no matter how long it is. To add to the end, set the next slot: team[#team + 1] = "yellow", or use table.insert(team, "yellow"), whichever reads cleaner to you.

Now make it yours: print the count of fruits, then print the first fruit. Once that works, add a fourth fruit and reprint the count to prove it grew.

Output
Click Run to see your output.