lua-users home
lua-l archive

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

>From: David Jeske <>
>Is there any way to do assignment inside an expression? I often want
>to do this in if or while blocks and whenever I try something like:
>if ((a = fn()) == 2) then
>while (a = fn()) do
>it (of course) dosn't work.. Is there some way to do assignment inside
>an expression like this, and if not, why not?

assignments are statements, not expressions as in C.
but, if 'a' is a global variable, you can d:

  while (setglobal("a",f())==2) do ... end

mind you, setglobal("a",f()) is probably *not* noticably slower than a=f()!