lua-users home
lua-l archive

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


On 20/06/2011 06:54, PROXiCiDE wrote:
Is it possible to crunch these repeatable statements in Lua? with out the
rewrite "IF" statements etc.. maybe inside Array/Table checking etc

local ops =
{
  ['>'] = function (w, n) return w > n end,
  ['>='] = function (w, n) return w >= n end,
  ['<'] = function (w, n) return w < n end,
  ['<='] = function (w, n) return w <= n end,
  ['=='] = function (w, n) return w == n end,
}
// Very primitive test...
local exp = '<='
local what = 15
local number = 10

for k, v in pairs(ops) do
  if k == exp then
    local b = v(what, number)
    print("Found with " .. exp)
    if b then print("OK") else print("NOK") end
    break
  end
end

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --