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
Question about for loops
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Tue Jul 04, 2023 5:29 am
Question about for loops
Last edited by SquareFish on Tue Jul 04, 2023 7:50 pm, edited 1 time in total.
Re: Question about for loops
Wrong:
Good:
Also good:
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
Code: Select all
for i = #zombies, 1, -1 do -- backwards from last to first
zombies[i] = nil
end
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
-
- Prole
- Posts: 2
- Joined: Tue Jul 04, 2023 5:29 am
Re: Question about for loops
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
Thats what [ code ] [/ code] tags are for (without spaces in [])SquareFish wrote: ↑Tue Jul 04, 2023 7:56 pm Thanks, I realized that the forum text editor removed the [ i ]
Re: Question about for loops
Extra question: what happens?
Code: Select all
for i = 1, 3 do
i = 4
print (i)
end
Re: Question about for loops
Here's the OP's code again surrounded with proper [code]...[/code] tags:
The answer to understand what's going on in numeric loops and predict the behaviour is in the manual's section 2.4.5.
(Edited after realizing that I misunderstood a few things)
Code: Select all
for i,z in ipairs(zombies) do
zombies[i] = nil (1)
z = nil (2)
end
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.More precisely, a for statement like
is equivalent to the code:Code: Select all
for v = e1, e2, e3 do block end
[...]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.
(Edited after realizing that I misunderstood a few things)
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests