lua-users home
lua-l archive

Re: Shorten long boolean IF conditions?

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


Daniel Hertrich wrote:
> I'd like to shorten an if condition such as:
> if strCustomPropertyValue == "inbox" or strCustomPropertyValue ==
> "today" or strCustomPropertyValue == "thisweek" or
> strCustomPropertyValue == "thismonth" or strCustomPropertyValue ==
> "thisyear" or strCustomPropertyValue == "later" or
> strCustomPropertyValue == "someday" or strCustomPropertyValue ==
> "archive" or strCustomPropertyValue == "otherpersons" then
> ...
> end
An alternative to the solution with tables is a vararg function:
function is_one_of(value, ...)
 for k, v in ipairs(arg) do
 if v == value then return true end
 end
end
You can use it like this:
if is_one_of(strCustomPropertyValue,
 "inbox", "thisweek", "thismonth", "thisyear", "later",
 "someday", "archive", "otherpersons") then
 ...
end
-- Gregory

AltStyle によって変換されたページ (->オリジナル) /