Chapter I ·
Language basics
/
Lesson 6 of 27
Iteration
You'll learn: Use
pairs for hash walks, ipairs for sequences, and write your own iterator.
ipairs walks the array part from 1 and stops at the first
nil. pairs walks every key in unspecified order. The generic
for accepts any *iterator function* — range(n) below shows
how to write one yourself: return (iter, state, control) and
Lua takes care of the rest.
Output
idle
Hit Run to execute.
Try it:
Add a
step parameter to range so range(10, 2) yields 1, 3, 5, 7, 9 with their squares.