Page 1 of 1
Question about for loops
Posted: Tue Jul 04, 2023 6:07 am
by SquareFish
I have this code here for removing a bunch of zombies in the zombies table, each of the values in zombies is a individual zombie table:
for i,z in ipairs(zombies) do
zombies [ i ] = nil (1)
z = nil (2)
end
My question is, why does line (1) work and line (2) doesn't? When I try line 2 nothing happens. So far I have the understanding that using z is like using a for each loop, and it points to the first value/object in the table, then the next, and so on. I was just wondering why z = nil doesn't work as opposed to zombies = nil. My best guess is the fact that it has to do with z not wanting to be null because it has another index in the loop to go on to or something like that
Thanks a lot guys, I never use for each loops in java so they're pretty confusing to me with lua
Re: Question about for loops
Posted: Tue Jul 04, 2023 1:43 pm
by darkfrei
Wrong:
Code: Select all
for i,z in ipairs(zombies) do
zombies = nil -- the table zombies does not exist anymore
z = nil -- local / global z does not exist
end
Good:
Code: Select all
for i = #zombies, 1, -1 do -- backwards from last to first
zombies[i] = nil
end
Also good:
Code: Select all
for i, z in ipairs(zombies) do
zombies[i] = zombies[#zombies] -- the i-th zombie is gone, now the i-th zombie is exactly-same as last
zombies[#zombies] = nil -- remove the last
end
Re: Question about for loops
Posted: Tue Jul 04, 2023 7:56 pm
by SquareFish
Thanks, I realized that the forum text editor removed the [ i ] that I added after the zombies on line one, I edited my post to add it in. Thanks a lot though, both these options make sense. I've had a lot of trouble with removing objects in a list in the past and for some reason it never occurred to me to just iterate backwards.
Re: Question about for loops
Posted: Wed Jul 05, 2023 6:51 am
by Andlac028
SquareFish wrote: ↑Tue Jul 04, 2023 7:56 pm
Thanks, I realized that the forum text editor removed the [ i ]
Thats what [ code ] [/ code] tags are for (without spaces in [])
Re: Question about for loops
Posted: Wed Jul 05, 2023 7:09 am
by darkfrei
Extra question: what happens?
Code: Select all
for i = 1, 3 do
i = 4
print (i)
end
Re: Question about for loops
Posted: Wed Jul 05, 2023 11:22 am
by pgimeno
Here's the OP's code again surrounded with proper [
code]...[
/code] tags:
Code: Select all
for i,z in ipairs(zombies) do
zombies[i] = nil (1)
z = nil (2)
end
The answer to understand what's going on in numeric loops and predict the behaviour is in the manual's section
2.4.5.
More precisely, a for statement like
is equivalent to the code:
Code: Select all
do
local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
if not (var and limit and step) then error() end
while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
local v = var
block
var = var + step
end
end
[...]
- var, limit, and step are invisible variables. The names shown here are for explanatory purposes only.
Note that altering
v (the loop variable) is
not going to influence the stop condition because it's stored in a new local with the same name after being assigned from
var.
(Edited after realizing that I misunderstood a few things)