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.
Janzo
Prole
Posts: 8
Joined: Sun Jul 31, 2016 1:20 am

Can someone please explain "for" statements in lua?

Post 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!
I'M SORRY, BUT DOES REALLY BOLD TEXT BOTHER YOU
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 »

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
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post by Jasoco »

The example you posted simply means that "blah blah blah" will be executed 100 times in a loop. That's literally it.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Last edited by zorg on Tue Aug 02, 2016 3:55 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

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

Post 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.
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 »

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.
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
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:
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:

Code: Select all

for i in 1100 do print(i) end
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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:

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].
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 2 guests