Using "or" operator

I want to run a which is depending on the value of local defined variable, let say XYZ
This variable can have various states, let say that interesting for me are 1, 3, 7.
I want to build a condition that if XYZ is 1 or 3 or 7 then a code is executed.

It works with the syntax like:

]if XYZ == 1 or XYZ == 3 or XYZ == 7 then...

but maybe there is a simplier solution?

I’ve tried:

if XYZ == (1 or 3 or 7)

but this doesn’t work.

No, that doesn’t work. If in doubt, refer to the Lua Reference Manual: Lua 5.1 Reference Manual - contents

However, a neater way to do this is:

local valid = {1, nil, 1, [7] = 1}   --- many ways to create this structure

if  valid[XYZ] then ...

You just have to be sure that XYZ is a number and not a string. To be sure, you could write:

if  valid[tonumber(XYZ)] then ...

…or you could change the structure itself to be valid for strings also.

Thanks, also for the reference documentation.

I still struggle to understand this part:

local valid = {1, nil, 1, [7] = 1} 

should I put here values in which I am interested, like:

local valid = {1, 3, 7}?

I don’t know where to find that kind of structure in the reference documentation :frowning:

The array proposal is to create an array where each entry lines up with one you are checking and gives you the answer.

Expanded fully, it looks like this:

local valid = {[1] = 1, [2] = nil, [3] = 1, [7] = 1}

You can put [test value] = answer for all the values you need to check (and for ones you don’t specify, the answer will be nil).

To be honest, just use the OR statement for this small number of values, and only do something like this if you have a lot (say, more than one line of values in the IF statement).

It’s here: Table Constructors

@rge is quite correct. As I mentioned, there are many ways to create this structure. Recall that any non-nil value is treated as a logical ‘true’, so it doesn’t matter what value you give the table elements.

In full, both methods shown already are equivalent to:

local valid = {}
valid[1] = 1
valid[3] = 1
valid[7] = 1

Perhaps the clearest way to define what you need is, in fact:

local valid = {}
local states = {1,3,7}   -- put the numbers you want here
for _,x in ipairs(states) do
  valid[x] = 1
end

You should also, perhaps, take a look at the logical operators. The original expression you wanted to try:

1 or 3 or 7

is always equal to 1.

OK, i understand it better, but then shouldnt:

if  valid[tonumber(XYZ)] then ...

be more like

[code]if valid[tonumber(XYZ)] == 1 then …[/quote]

?[/code]

[quote=“kwieto, post:6, topic:195562”]but then shouldnt:

be more like
…[/quote]

Either will do…

careful - nil and false are both false, anything else is true

Comment from a non programmer. :-\

I find the ]if XYZ == 1 or XYZ == 3 or XYZ == 7 then… version much easier to read.

Not so neat but easier to debug??

Yes, agreed. Easier for just three values, but doesn’t scale very well to more.

However, I do find the if valid[x] then... syntax very readable once the variable is set up.

I agree that a line with lots of OR functions spreading over multiple lines of code could get a bit cumbersome.

Or if you have this pattern in multiple places, use a function that iterates over the list of things to check against:

isoneof returns true if the first parameter matches any of the others, or false if not

[code]local function isoneof (val, …)
for i,v in ipairs(arg) do
if (val == v) then
return true
end
end

return false
end
[/code]

So your line would become

if isoneof(XYZ, 1, 3, 7) then

Note - it is type sensitive, so you may still need the numeric conversion etc.

I believe you mean:

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

  return false
end

Wow, see what I mean for us non programmers. the difference between () and {} :stuck_out_tongue:

@Slartibartfast
Some advantage of building up a function is that you can integrate it into startup lua, then call it every time you need from a various scenes, just defining the parameters according to the needs.

Then the code within a specific scene is more “elegant”, I would say.
I am also not a programmer so I am a bit “blind” here, but anyhow I want to know better ways to do something if they are.

I had a ‘programmer’ working for me once who said:

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

Didn’t last for long!

[quote=“akbooer, post:5, topic:195562”]You should also, perhaps, take a look at the logical operators. The original expression you wanted to try:

1 or 3 or 7

is always equal to 1.[/quote]

What you mean, exactly?

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 …

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

[quote=“akbooer, post:13, topic:195562”]I believe you mean:

[code]
local function isoneof (val, …)
for i,v in ipairs {…} do
if (val == v) then
return true
end
end

return false
end
[/code][/quote]

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