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

boolean and numbers

Started by 1wsx10, 05 February 2016 - 12:00 PM
1wsx10 #1
Posted 05 February 2016 - 01:00 PM
im still pretty new to lua so some things like booleans and numbers are new to me.

i have searched elsewhere and this is what i come up with:

- nil returns false
- booleans return it's value
- other values return true

but that does not explain this line of code:

local at = a[l] + ((w[ln] == nil) and d or 1)

w is a table of numbers, d is a boolean, a is another table of numbers, at is a number

so, how does it work?
Edited on 05 February 2016 - 12:02 PM
Lupus590 #2
Posted 05 February 2016 - 01:08 PM
 local at = a[l] + ((w[ln] == nil) and d or 1)

is a short hand for several if statement checks (infact it's not just shorter for use reading it, but also quicker for Lua to 'read' too)

I'll try to expand it for you:
local at = nil
if  (w[ln] == nil) and d then
  at = a[l]
else
  at =  a[l] + 1
end
Edited on 05 February 2016 - 12:09 PM
Wojbie #3
Posted 05 February 2016 - 01:10 PM
 local at = a[l] + ((w[ln] == nil) and d or 1)

is a short hand for several if statement checks (infact it's not just shorter for use reading it, but also quicker for Lua to 'read' too)

I'll try to expand it for you:
local at = nil
if  (w[ln] == nil) and d then
  at = a[l]
else
  at =  a[l] + 1
end

Correction. It is:

local at = nil
if  (w[ln] == nil) then
  at = a[l] + d
else
  at = a[l] + 1
end
You missed order of parentheses.
Edited on 05 February 2016 - 12:13 PM
1wsx10 #4
Posted 05 February 2016 - 01:25 PM
thanks.

weird how lua excludes a++ but then includes stuff like this

edit:

so its luas version of a conditional statement? in C# it would be something like:

int at = a[l] + (w[ln] == null) ? d : 1

edit2:

dont worry, i found this: http://www.lua.org/pil/3.3.html
Edited on 05 February 2016 - 01:04 PM
Bomb Bloke #5
Posted 07 February 2016 - 06:24 AM
so its luas version of a conditional statement? in C# it would be something like:

It tends to be used in Lua code to make up for the lack of a proper ternary, but it's also possible in other languages.