Author Topic: Using "or" operator  (Read 766 times)

Offline akbooer

  • Master Member
  • *******
  • Posts: 4947
  • Karma: +211/-67
  • "Less is more"
Re: Using "or" operator
« Reply #15 on: February 21, 2017, 09:45:27 am »
I had a 'programmer' working for me once who said:

Quote
I can't believe it doesn't work... I only changed one line."

Didn't last for long!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline kwieto

  • Jr. Member
  • **
  • Posts: 66
  • Karma: +2/-2
Re: Using "or" operator
« Reply #16 on: February 21, 2017, 09:46:42 am »
You should also, perhaps, take a look at the logical operators.  The original expression you wanted to try:

Code: [Select]
1 or 3 or 7
is always equal to 1.

What you mean, exactly?

Offline akbooer

  • Master Member
  • *******
  • Posts: 4947
  • Karma: +211/-67
  • "Less is more"
Re: Using "or" operator
« Reply #17 on: February 21, 2017, 09:53:02 am »
What you mean, exactly?

Code: [Select]
local x = 1 or 3 or 7
print(x)

... will always print out '1', because the concatenated 'or' operator chooses the first non-nil value.  Although, indeed, @rge is correct about the treatment of 'false', so ...

Code: [Select]
local x = false or 42
print(x)

... will always print out '42'.

For an easier guide to Lua than the reference manual, try: Programming in Lua

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline rge

  • Jr. Member
  • **
  • Posts: 82
  • Karma: +7/-0
Re: Using "or" operator
« Reply #18 on: February 21, 2017, 09:55:27 am »
I believe you mean:

Code: [Select]
local function isoneof (val, ...)
  for i,v in ipairs {...} do
    if (val == v) then
      return true
    end
  end

  return false
end

Thanks, I wasn't aware the syntax changed from the old arg variable - at least now I won't keep using "args" by accident!