Lua/Corona "OR" operator

I am working on an app the returns a json table from a network request. When the user enters a 3 letter airport code and it gets returned in a newText object. If the type in the airport LAX the stateCode from the json file gets returned as CA but if the type in an airport code from a different country its returns an error because what has been returned doesnt have a stateCode. So i tried to use the “or” operator in the following code.

Local display.newText(myData.airports[1].stateCode or myData.airports[1].countryCode)

How can I return the stateCode and if it doenst have a stateCode, return the countryCode?

Are you saying the example line of code you showed did not work? 

If so, just break it apart:

local myData = myData.airports[1].stateCode or myData.airports[1].countryCode local display.newText(myData)

Give that a try and see what happens.

 Jay

It may depend on what ‘doesn’t have a state code’ means. What you actually are testing for is this stateCode being ‘nil’ - not (say) an empty string. I would put 

print(myData.airports,myData.airports[1],myData.airports[1].stateCode,myData.airports[1],countryCode) 

to see exactly what is coming out. It may well be an empty string.

@MohawkMan thanks. That worked perfectly. @paulscottrobson oh. Ok so I checked the data and it does return a string. I do want it to return nil or ‘empty’. Should my code read: local stateCodeText = myData.airports[1].stateCode or “” local myText = display.newText(stateCodeText, 0, 0, font, 16) ??

TBH that’s probably over using or. I’d be inclined to write :

local text = myData.airports[1].stateCode if text == nil or text == "" then text = myData.airports[1].whateverItwas or "" end local myText = display.newText(.....)

Things like that can introduce subtle bugs where this sort of answer is cleaner and more reliable. I tend to use or only for default parameter values (excepting logical or obviously :wink: )

One-liner:

[lua]

display.newText((myData.airports[1].stateCode ~= “” and myData.airports[1].stateCode) or myData.airports[1].countryCode)

[/lua]

The Lua “(a and b) or c” is in most cases equivalent to most other languages’ “a ? b : c” operator.

  • C

Indeed. Though as with the a?b:c operator the minimal speed gains in code (if any) and code size probably don’t make its regular use worth the risk.

Are you saying the example line of code you showed did not work? 

If so, just break it apart:

local myData = myData.airports[1].stateCode or myData.airports[1].countryCode local display.newText(myData)

Give that a try and see what happens.

 Jay

It may depend on what ‘doesn’t have a state code’ means. What you actually are testing for is this stateCode being ‘nil’ - not (say) an empty string. I would put 

print(myData.airports,myData.airports[1],myData.airports[1].stateCode,myData.airports[1],countryCode) 

to see exactly what is coming out. It may well be an empty string.

@MohawkMan thanks. That worked perfectly. @paulscottrobson oh. Ok so I checked the data and it does return a string. I do want it to return nil or ‘empty’. Should my code read: local stateCodeText = myData.airports[1].stateCode or “” local myText = display.newText(stateCodeText, 0, 0, font, 16) ??

TBH that’s probably over using or. I’d be inclined to write :

local text = myData.airports[1].stateCode if text == nil or text == "" then text = myData.airports[1].whateverItwas or "" end local myText = display.newText(.....)

Things like that can introduce subtle bugs where this sort of answer is cleaner and more reliable. I tend to use or only for default parameter values (excepting logical or obviously :wink: )

One-liner:

[lua]

display.newText((myData.airports[1].stateCode ~= “” and myData.airports[1].stateCode) or myData.airports[1].countryCode)

[/lua]

The Lua “(a and b) or c” is in most cases equivalent to most other languages’ “a ? b : c” operator.

  • C

Indeed. Though as with the a?b:c operator the minimal speed gains in code (if any) and code size probably don’t make its regular use worth the risk.