lua-users home
lua-l archive

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


Asko Kauppi wrote:
> Alexandr Leykin kirjoitti 11.11.2007 kello 22:18:
>> How in the Lua create small "if-in" statement e.g.
>> 
>> if method in ('HEAD','GET','POST') then ...
>> 
>> now it's see:
>> 
>> if method =='HEAD' or method=='GET' or method=='POST' then ...
> 
>  > method="GET"
>  > if ({ HEAD=true, GET=true, POST=true }) [method] then print "Hey!"
> 
> The extra parentheses are required for a syntactic reason.

You can create a simple 'in' function:

function In(t)
    local s = {}
    for _,v in pairs(t) do
        s[v] = true
    end
    return function(value)
        return s[value] or false
    end
end

if In {'HEAD','GET','POST'} (method) then
   dosomething()
end

You can't use all lowercase 'in' since it's a Lua keyword.

Also your original syntax could be supported with a token filter.