Can someone please explain "for" statements in lua?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Can someone please explain "for" statements in lua?

Post by CanadianGamer »

Nixola wrote:
CanadianGamer wrote:I that's not what I mean by iterating through an integer.
This doesn't mean it's not.
(EDIT from here on)
Furthermore, that is exactly what OP wrote:
Janzo wrote:

Code: Select all

for i = 1,100 do
blah blah blah
end
One more thing, you don't iterate over tables, you iterate over iterators. This:

Code: Select all

for i in 1100 do print(i) end
is as broken as this:

Code: Select all

for i in {"foo", "bar", 6} do print(i) end
Both will raise an error. Almost the same error, in fact. Attempt to call a number/table value.

Before you claim you have to use ipairs/pairs to iterate over a table, you can do the same with a number. You can even monkeypatch ipairs itself:

Code: Select all

do
  local oldipairs = ipairs
  ipairs = function(n)
    if type(n) == "number" then
      local i = 0
      return function() i = i + 1; if i <= n then return i end end
    end
    return oldipairs(n)
  end
end

for i in ipairs(6) do print(i) end
>1
>2
>3
>4
>5
>6
The numeric for loop form iterates over a number, the generic for loop form iterates over values returned by an iterator. You don't need a table, and in fact Lua includes at least two other iterators: [manual]io.lines[/manual] and [manual]string.gmatch[/manual].

That is what I meant. I'm sorry I wasn't clear enough.
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
pgimeno
Party member
Posts: 3686
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can someone please explain "for" statements in lua?

Post by pgimeno »

Well, technically you can't do

Code: Select all

for i in {"foo", "bar", 6} do print(i) end
but you can do

Code: Select all

for i in next, {"foo", "bar", 6} do print(i) end
(will print 1, 2, 3)

But that won't help the OP.

'for' loops have two forms: the numeric form and the iterator form.

In some languages like BASIC and Pascal, you would write 'for i = 1 to 100' (BASIC) or 'for i := 1 to 100 do' (Pascal). That's somewhat more expressive, and means that the loop will be done 100 times and the variable 'i' will take the values 1, 2, ... up to 100 inclusive. In Lua, it's the same idea, but the syntax requires you to write a comma instead of the word 'to': 'for i = 1, 100 do' means the next part until the matching 'end' will be executed 100 times, with the variable 'i' taking the values from 1 to 100, a different one each turn, in that order. A Lua quirk is that the loop variable, 'i' in this case, is local to the loop, and therefore not available outside the loop.

That's the numeric form. The iterator form is more complicated, and for most practical purposes, all you need to know is that if you're told that a certain function is an iterator, then you can do: 'for v1, v2, v3... in iterator_function(args) do' in order to iterate over whatever the iterator iterates through. For example, pairs() iterates over table keys and values; [wiki]love.filesystem.lines[/wiki]() iterates over the lines of a given file.

Only if you want to create iterators, you need to go deeper into the details. It's probably not worth it, though.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 6 guests