Page 1 of 2
Can someone please explain "for" statements in lua?
Posted: Tue Aug 02, 2016 2:45 am
by Janzo
I've been using love for a little bit of time now, and i'm quite familiar with lua. However there is this one thing i have never grasped. Those freaking for statements.
i see things like
Code: Select all
for i = 1,100 do
blah blah blah
end
all the time, and i really can't seem to understand it
can someone explain it to me? thanks!
Re: Can someone please explain "for" statements in lua?
Posted: Tue Aug 02, 2016 2:57 am
by CanadianGamer
You can't iterate through an integer in lua. You can only loop through data types that support it such as tables.
For example.
Code: Select all
function love.load()
--define example table
exampleTable = {}
end
function love.update()
end
function love.draw()
for i, enemy in ipairs(exampleTable) do
--whatever it is you want to do with the information in exampleTable
end
end
Re: Can someone please explain "for" statements in lua?
Posted: Tue Aug 02, 2016 3:37 am
by Jasoco
The example you posted simply means that "blah blah blah" will be executed 100 times in a loop. That's literally it.
Re: Can someone please explain "for" statements in lua?
Posted: Tue Aug 02, 2016 3:41 am
by zorg
CanadianGamer wrote:You can't iterate through an integer in lua.
Bull.
Code: Select all
-- Simple numeric loop.
-- a is the number the loop should start at
-- b is the number it should end at (inclusive)
-- c is the step, meaning how many to add (or subtract, in case it's negative) from a until it reaches b. If you don't give this, it's 1 by default
-- i is the loop index, that changes inside the loop, that you can use there to keep track which iteration the loop is in.
for i=a, b, c do
print(i) -- if a,b,c == 1,2,1 it will print two lines, 1 on the first, 2 on the second.
end
Code: Select all
-- A bit more advanced, this is an iterator, meaning it takes a table t, and goes through all the -numerical- indices inside (meaning all elements whose keys are in a sequence (meaning starting from 1, all following integers (whole numbers) until the first "gap")).
-- i is the current index of the table elements, that again, changes inside the loop body
-- v is the value that also changes, always equal to t[i]
local t = {'x','y','z'}; t[5] = 'w'
for i,v in ipairs(t) do
print(i,v) -- will print the pairs 1,x; 2,y; 3,z; but since the 4th doesn' exist, it won't print the 5th.
end
Code: Select all
-- This is similar to the ipairs iterator above, but this will go through every existing key in a table, be it numeric or strings or anything else.
-- k is the current key, changing in the loop body as usual, basically same as the i index as above, but it can be almost anything.
-- v is similarly t[k], also changes in the body of the loop
local t = {}; t.abc = '1'; t.xyz = 1; t[50] = 'qwertz'; t[function() return 'whatever' end] = 'this works too'; t[true] = 'YES'
for k,v in pairs(t) do
print(k,v) -- will print -all- of the above key,value pairs. the order is arbitrary!!!
end
All of the above work.
Re: Can someone please explain "for" statements in lua?
Posted: Tue Aug 02, 2016 3:45 am
by Jasoco
One thing to remember about pairs though is that the order it runs through is not predictable. So don't rely on it to execute code in a specific order, even if you add items to the table in the same order every time. Even if your table includes a table.a, table.b and table.c, it may not do a, b, c. Then again it might. It's never guaranteed.
Re: Can someone please explain "for" statements in lua?
Posted: Tue Aug 02, 2016 4:14 am
by airstruck
Janzo wrote:I've been using love for a little bit of time now, and i'm quite familiar with lua. However there is this one thing i have never grasped. Those freaking for statements.
Lolz.
Have you tried reading the
section of the Lua manual about for statements? It's only about a page long.
The Lua manual sometimes uses very concise language and packs a lot of information into just a few words, but it explains everything you need to know if you take the time to think about it and understand it. A thread like this isn't going to tell you anything the manual wouldn't already have told you. Seriously,
read the manual.
Re: Can someone please explain "for" statements in lua?
Posted: Wed Aug 03, 2016 4:33 pm
by CanadianGamer
zorg wrote:CanadianGamer wrote:You can't iterate through an integer in lua.
Bull.
None of the example you gave are iterating through integers.
If you try to iterate through an integer like 6 or 1100 you'll get an error.
Re: Can someone please explain "for" statements in lua?
Posted: Wed Aug 03, 2016 4:52 pm
by Nixola
CanadianGamer wrote:zorg wrote:CanadianGamer wrote:You can't iterate through an integer in lua.
Bull.
None of the example you gave are iterating through integers.
If you try to iterate through an integer like 6 or 1100 you'll get an error.
Code: Select all
for i = 1, 6 do print(i) end
for i = 1, 1000 do print(i) end
for i = 1000, 6, -1 do print(i) end
Re: Can someone please explain "for" statements in lua?
Posted: Wed Aug 03, 2016 5:14 pm
by CanadianGamer
Nixola wrote:CanadianGamer wrote:zorg wrote:
Bull.
None of the example you gave are iterating through integers.
If you try to iterate through an integer like 6 or 1100 you'll get an error.
Code: Select all
for i = 1, 6 do print(i) end
for i = 1, 1000 do print(i) end
for i = 1000, 6, -1 do print(i) end
I that's not what I mean by iterating through an integer.
I mean something like this:
Re: Can someone please explain "for" statements in lua?
Posted: Wed Aug 03, 2016 5:33 pm
by Nixola
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:
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].