lua-users home
lua-l archive

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


On Sat, Nov 20, 2010 at 7:40 AM, Axel Kittenberger <axkibe@gmail.com> wrote:
>> Or just mutate the iterator inside the loop:
>>  for a in it do if a == '-f' then name = it()  .....
> Really may I? I read from the manual that I may not.
> - how do you imagine to skip a pair?
> for k, v in ipairs(x) do .....

Are you thinking of the comment "The behavior of next is undefined if,
during the traversal, you assign any value to a non-existent field in
the table" concerning next/pairs?  That doesn't apply to this:

  > local x=0; function it() x=x+1; if x<=6 then return x end end; for
y in it do print(y,it()) end
  1	2
  3	4
  5	6

However, I don't see a simple way to artificially advance a stateless
iterator like returned by ipairs--see code in "A for statement like
.... is equivalent to the code" in the manual--but you can wrap ipairs
in your own stateful iterator like the above.