[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: breaking out of a while loop
- From: Marcus Mason <m4gicks@...>
- Date: Sat, 28 May 2022 02:53:21 +0100
> A “smart” alternative would be this loop:
> while true do
> local k = next(t)
> if not k then break end
> t[k] = nil
The reason this code works is because `next` returns `nil` when there is no key. If you check my previous post you'll see the alternative condition you'd need to write this style of loop.
I would also point out that the quoted code from Lua performance tips is specifically referring to the pairs iteration above it, and is not really a general thing. I think your original code with the condition in the while loop – the one which starts `while bottle_count ~= 0 do` – is fine, and I'm not sure there's a performance difference here either.