lua-users home
lua-l archive

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


> Is it the case that the only way to express the *invalid* form:
>    while x = foo() do print(x) end
> is by using the valid Lua form:
>    while (function() x=foo(); return x end)() do print(x) end

No, if you expect x to be local to the body of the loop, you can write:

  for x in foo do print(x) end

(As long as foo is a real function and not a table with a __call method)

You can generalise that by writing functions in the for statement, if that
suits your fancy:

for x
  in function() local line = io.read()
                return (not string.find(line, "^%s*quit")) and line
     end
  do
    local _, _, command, rest = string.find(x, "^%s*(%w+)%s*(.*)")
    if command and Command[command] do
      Command[command](split(rest))
    else
      io.write(string.format("I don't know how to '%s'\n", command or x))
    end
  end
end