attempt to compare number with string

Not sure if it’s a lua bug or a corona bug… Or if it’s know and not considered a bug… Comparing a string to a number yelds an error:
function main()
local x = “1”

print(x+0>1) --this works
print(x>1) --this yelds an error “attempt to compare number with string”
end
main()

I thought any arithmetic operation would convert strings to numbers… From this reference: http://developer.anscamobile.com/demo/APIReference.pdf

“Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules.”
Comments will be appreciated :slight_smile:

thank you,
Bernardo at BogeyBox [import]uid: 79152 topic_id: 16340 reply_id: 316340[/import]

The automatic coercion works when Lua knows the context of the code.

The first line works because you are adding 0 to the variable so Lua can guess what you want to do and convert the string to a number, then you compare it all good. For the second line Lua doesn’t know what you are doing so doesn’t convert it.

You should use these functions to do the conversion to make sure it is done:

http://pgl.yoyo.org/luai/i/tonumber

http://pgl.yoyo.org/luai/i/tostring [import]uid: 5833 topic_id: 16340 reply_id: 60895[/import]