lua-users home
lua-l archive

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


I've been considering lua and playing with it
from time to time but am confused about something...

Is there a way to make the counter/index variable
used in the for loop accessible outside the for loop
without having to set the outside var to the index
at each iteration? Doing that makes for only as fast
as while, and is generally annoying regardless.

Enough talk, here's code...


  function F(input)
    return input + 1
  end
  
  i=-1
  -- while (i < 10000000) do
  -- speed was 8s with while, but is 3s with for
  for i=1,10000000 do
    i = F(i)
  end
  
  print("DONE!")
  print(i)
  -- prints -1 here, but prints 10000000 with the while loop

What do I need to do short of x=i in the for loop,
where x is declared outside.