7 posts
Posted 31 January 2013 - 04:16 PM
In mathematics, you can use 4<x<7 to find out if x is equal to 5 or 6. As another example 2<x<9 is true if x is anything from 3 to 8. My question is: can you do this kind of thing as the parameter for if statements? I know about using "and", however I was trying to quickly detect whether someone clicked in the right spot to activate a button on a monitor.
Doing the whole "if (x >= 4) and (x <= 6) and (y >= 5) and (y <= 7) then" is just annoying. I'd prefer to be able to do something like "if (3<x<7) and (4<y<8) then". It's a lot simpler and quicker to type.
8543 posts
Posted 31 January 2013 - 04:20 PM
No.
7 posts
Posted 31 January 2013 - 04:22 PM
Oh well. Thanks for helping.
7508 posts
Location
Australia
Posted 31 January 2013 - 04:28 PM
To expand on "No." … sadly lua does not support this functionality and there is no way to actually program this in as Java also does not support this functionality either. however if you wish to simplify it a little you could write a function for this:
local function inBounds(var, min, max)
return var >= min and var <= max
end
which this would simplify the if statements making them this:
if inBounds(x,4,7) then
-- do something
elseif inBounds(x,2,9) then
-- do something
end
96 posts
Posted 01 February 2013 - 04:07 AM
Doing the whole "if (x >= 4) and (x <= 6) and (y >= 5) and (y <= 7) then" is just annoying. I'd prefer to be able to do something like "if (3<x<7) and (4<y<8) then". It's a lot simpler and quicker to type.
There aren't many programming languages that offer that syntax. Perl 6 is one. It also has "x = 4 or 5" which is somewhat unusual in languages, although COBOL also has it. But not Lua.
2005 posts
Posted 01 February 2013 - 11:57 AM
Lua supports that syntax, but gives it a different (and far more generally useful, in my opinion) meaning.