Is there a way to shorten this if or statement?

Hi,
I have a function that checks if the correct button is pushed. There are 5 buttons that are correct and 5 that are wrong.

I would write this like this

if x1==true or x2==true or x3 == true or x4==true or x5==true then  
print ("some text")  
end  

Is there a shorter way to write this?
something like

if x1 or x2 or x3 or x4 or x5 == true then  
.....  

Just being curious. The original line of code is not that bad, but just trying to learn on shortening my code. [import]uid: 100901 topic_id: 36051 reply_id: 336051[/import]

no need for the ==true (someone else correct me if I’m wrong but I can’t think of where it would be different than leaving it out)

so I’d do

if (x1 or x2 or x3 or x4 or x5) then .. end [import]uid: 32462 topic_id: 36051 reply_id: 143211[/import]

Ok thanks,
but what if I make a different comparison instead of true?

if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then  

This is the whole piece of code I am using:

[code]
local function MoveWord(event)
local t = event.target

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
beginX=t.x
beginY=t.y
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then

if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then
if t.x >185 and t.x<320 and t.y >17 and t.y < 170 then
print (“antwoord is goed”)
event.target:removeSelf()
answer=false
end
else
t.x=beginX
t.y=beginY
print(“antwoord is fout”)
– PlayAudioError()
end
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
[/code] [import]uid: 100901 topic_id: 36051 reply_id: 143212[/import]

if that’s something you’ll be doing on a reg basis maybe make a helper function.

this is pseudocode but something like

tableContainsVal=function(t,valToLookFor)  
 local valToReturn=false  
 for k,v in ipairs(t) do  
 if v==valToLookFor then  
 valToReturn=true  
 end  
 end  
return valToReturn  
end  

then you call it like

 if (tableContainsVal( {goed.g1,goed.g2,goed.g3,goed.g4,goed.g5}, event.target) then  
 ...  
 end  

I keep a bunch of functions for things like this in a module called util.lua and if you did that you could call it as util.tableContainsVal
[import]uid: 32462 topic_id: 36051 reply_id: 143216[/import]

no need for the ==true (someone else correct me if I’m wrong but I can’t think of where it would be different than leaving it out)

so I’d do

if (x1 or x2 or x3 or x4 or x5) then .. end [import]uid: 32462 topic_id: 36051 reply_id: 143211[/import]

Ok thanks,
but what if I make a different comparison instead of true?

if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then  

This is the whole piece of code I am using:

[code]
local function MoveWord(event)
local t = event.target

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
beginX=t.x
beginY=t.y
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then

if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then
if t.x >185 and t.x<320 and t.y >17 and t.y < 170 then
print (“antwoord is goed”)
event.target:removeSelf()
answer=false
end
else
t.x=beginX
t.y=beginY
print(“antwoord is fout”)
– PlayAudioError()
end
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
[/code] [import]uid: 100901 topic_id: 36051 reply_id: 143212[/import]

if that’s something you’ll be doing on a reg basis maybe make a helper function.

this is pseudocode but something like

tableContainsVal=function(t,valToLookFor)  
 local valToReturn=false  
 for k,v in ipairs(t) do  
 if v==valToLookFor then  
 valToReturn=true  
 end  
 end  
return valToReturn  
end  

then you call it like

 if (tableContainsVal( {goed.g1,goed.g2,goed.g3,goed.g4,goed.g5}, event.target) then  
 ...  
 end  

I keep a bunch of functions for things like this in a module called util.lua and if you did that you could call it as util.tableContainsVal
[import]uid: 32462 topic_id: 36051 reply_id: 143216[/import]

no need for the ==true (someone else correct me if I’m wrong but I can’t think of where it would be different than leaving it out)

so I’d do

if (x1 or x2 or x3 or x4 or x5) then .. end [import]uid: 32462 topic_id: 36051 reply_id: 143211[/import]

Ok thanks,
but what if I make a different comparison instead of true?

if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then  

This is the whole piece of code I am using:

[code]
local function MoveWord(event)
local t = event.target

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
beginX=t.x
beginY=t.y
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then

if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then
if t.x >185 and t.x<320 and t.y >17 and t.y < 170 then
print (“antwoord is goed”)
event.target:removeSelf()
answer=false
end
else
t.x=beginX
t.y=beginY
print(“antwoord is fout”)
– PlayAudioError()
end
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
[/code] [import]uid: 100901 topic_id: 36051 reply_id: 143212[/import]

if that’s something you’ll be doing on a reg basis maybe make a helper function.

this is pseudocode but something like

tableContainsVal=function(t,valToLookFor)  
 local valToReturn=false  
 for k,v in ipairs(t) do  
 if v==valToLookFor then  
 valToReturn=true  
 end  
 end  
return valToReturn  
end  

then you call it like

 if (tableContainsVal( {goed.g1,goed.g2,goed.g3,goed.g4,goed.g5}, event.target) then  
 ...  
 end  

I keep a bunch of functions for things like this in a module called util.lua and if you did that you could call it as util.tableContainsVal
[import]uid: 32462 topic_id: 36051 reply_id: 143216[/import]

no need for the ==true (someone else correct me if I’m wrong but I can’t think of where it would be different than leaving it out)

so I’d do

if (x1 or x2 or x3 or x4 or x5) then .. end [import]uid: 32462 topic_id: 36051 reply_id: 143211[/import]

Ok thanks,
but what if I make a different comparison instead of true?

if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then  

This is the whole piece of code I am using:

[code]
local function MoveWord(event)
local t = event.target

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
beginX=t.x
beginY=t.y
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then

if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
if event.target==goed.g1 or event.target==goed.g2 or event.target==goed.g3 or event.target==goed.g4 or event.target==goed.g5 then
if t.x >185 and t.x<320 and t.y >17 and t.y < 170 then
print (“antwoord is goed”)
event.target:removeSelf()
answer=false
end
else
t.x=beginX
t.y=beginY
print(“antwoord is fout”)
– PlayAudioError()
end
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
[/code] [import]uid: 100901 topic_id: 36051 reply_id: 143212[/import]

if that’s something you’ll be doing on a reg basis maybe make a helper function.

this is pseudocode but something like

tableContainsVal=function(t,valToLookFor)  
 local valToReturn=false  
 for k,v in ipairs(t) do  
 if v==valToLookFor then  
 valToReturn=true  
 end  
 end  
return valToReturn  
end  

then you call it like

 if (tableContainsVal( {goed.g1,goed.g2,goed.g3,goed.g4,goed.g5}, event.target) then  
 ...  
 end  

I keep a bunch of functions for things like this in a module called util.lua and if you did that you could call it as util.tableContainsVal
[import]uid: 32462 topic_id: 36051 reply_id: 143216[/import]