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

Shorten small if statements

Started by Mads, 16 October 2012 - 04:28 PM
Mads #1
Posted 16 October 2012 - 06:28 PM
Ever run into a case, where you need alot of these statementes:

local operatingSystem = ""
if os.getenv("APPDATA") ~= nil then
	operatingSystem = "windows"
else
	operatingSystem = "unix" -- or linux
end

Here is a little know way, which will allow you to make these small "if statements" a lot more compact, just like in C++

C++ example:

int biggest = (x > y ? x : y);

This can also be done in Lua. This is how:

local var = (statement and value_if_true or value_if_false)

So, the above example(checking the operating system), would look like this:

local operatingSystem = (os.getenv("APPDATA") ~= nil and "windows" or "unix")
PixelToast #2
Posted 16 October 2012 - 07:46 PM
i figured this out by myself because i knew that non nil and false will pass as true through logic as true and will return that value
nice tutorial :3
Mads #3
Posted 16 October 2012 - 08:48 PM
Thanks!
ChunLing #4
Posted 18 October 2012 - 06:03 AM
Shouldn't this be
local operatingSystem = (os.getenv("APPDATA") and "windows") or "unix"
?

It seems like functionally there is no difference due to 'and' having precedence over 'or', but the placement of the parenthesis in your example merely confuses this point.
Mads #5
Posted 21 October 2012 - 09:17 AM
@ChunLing No. That is just how it works. Look at PixelToast's first post.
Espen #6
Posted 21 October 2012 - 10:06 AM
Shouldn't this be
local operatingSystem = (os.getenv("APPDATA") and "windows") or "unix"
?

It seems like functionally there is no difference due to 'and' having precedence over 'or', but the placement of the parenthesis in your example merely confuses this point.

If os.getenv("APPDATA") returns nil, then the following two would both be of boolean value FALSE:

os.getenv("APPDATA")
os.getenv("APPDATA") ~= nil

If it returns any value that is not nil, then both of these would be of value TRUE.
So yes, you don't actually need the ~= nil part in the condition.

EDIT: Some further clarification:
Any value that is not nil will be interpreted as true when in a condition, but it won't necessarily be of value true if you'd directly compare it with a boolean value. That is because it doesn't actually return a boolean value, it's just that it's true that it isn't nil, if that makes any sense.^^

EDIT 2: This might be worded a bit better: http://www.lua.org/pil/2.2.html
No wordsmith me is. :)/>/>
Edited on 21 October 2012 - 08:19 AM
ChunLing #7
Posted 21 October 2012 - 11:42 AM
Sorry, I may not have been entirely clear. Because "and" has precedence over "or", you don't need the parenthesis at all.

But, for the sake of clarity, if you're going to use them they might as well emphasize that the "and" is performed before the "or".

Wordsmith wise, you forgot that false is a non nil value that evaluates to false :)/>/> This is a fairly minor point, the main difference being that assigning a value to nil causes it to no longer require memory, while assigning it to false does retain memory usage. Explicit false can be safer (or give a more certain result) in some cases for this reason, but in this case if os.getenv("APPDATA") isn't nil, then it isn't nil, and that's that.
Espen #8
Posted 21 October 2012 - 12:55 PM
I didn't explicitly mention it, you're right. But then again, false is a boolean value and I thought it'd be self-evident that false does not equal true. :)/>/>

EDIT: Reading your post again, it seems I missed your tongue-in-cheek smiley to mean exactly what such a smiley means to express. :)/>/> (-> me rolling my eyes about myself)
Mads #9
Posted 21 October 2012 - 03:00 PM
You do not need the parenthesis, I just like having them there.

Ohh, and AND has precendence over OR.

This:

return a > b and x > y or a > x and b > y

Would translate into this:

return (a > b and x > y) or (a > x and b > y)