This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tdawlings's profile picture

[Lua] [Question] Using things like 4<x<7?

Started by tdawlings, 31 January 2013 - 03:16 PM
tdawlings #1
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.
Lyqyd #2
Posted 31 January 2013 - 04:20 PM
No.
tdawlings #3
Posted 31 January 2013 - 04:22 PM
Oh well. Thanks for helping.
theoriginalbit #4
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
PhilHibbs #5
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.
ChunLing #6
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.