i see things like
Code: Select all
for i = 1,100 do
blah blah blah
end
can someone explain it to me? thanks!
Code: Select all
for i = 1,100 do
blah blah blah
end
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
Bull.CanadianGamer wrote:You can't iterate through an integer in lua.
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
Lolz.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.
None of the example you gave are iterating through integers.zorg wrote:Bull.CanadianGamer wrote:You can't iterate through an integer in lua.
CanadianGamer wrote:None of the example you gave are iterating through integers.zorg wrote:Bull.CanadianGamer wrote:You can't iterate through an integer in lua.
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.Nixola wrote:CanadianGamer wrote:None of the example you gave are iterating through integers.zorg wrote: Bull.
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
Code: Select all
for i in 1100 do print(i) end
This doesn't mean it's not.CanadianGamer wrote:I that's not what I mean by iterating through an integer.
One more thing, you don't iterate over tables, you iterate over iterators. This:Janzo wrote:Code: Select all
for i = 1,100 do blah blah blah end
Code: Select all
for i in 1100 do print(i) end
Code: Select all
for i in {"foo", "bar", 6} do print(i) end
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
Users browsing this forum: Ahrefs [Bot], Bing [Bot], slime and 5 guests