lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Nov 15, 2018 at 11:20 PM Dibyendu Majumdar wrote:
Hi, to be honest the for num loop variable should be treated as
'readonly' inside the loop body as it is a copy of the real index
variable, and changing it inside the body doesn't affect the loop, and
is just bad practice in my view.

IMO, it's not a "bad practice".
Mutability of loop variable (without affecting the loop) is quite useful.
Examples:

for name, value in str:gmatch"(%w+)=(%d+)" do
  value = tonumber(value)
  ....
end

for k = 1, 10 do
  k = tostring(k)
  ....
end