Jump to content




boolean and numbers


  • You cannot reply to this topic
4 replies to this topic

#1 1wsx10

  • Members
  • 14 posts
  • LocationMelbourne, Australia

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 by 1wsx10, 05 February 2016 - 01:02 PM.


#2 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

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 by Lupus590, 05 February 2016 - 01:09 PM.


#3 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 05 February 2016 - 01:10 PM

View PostLupus590, on 05 February 2016 - 01:08 PM, said:

 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 by Wojbie, 05 February 2016 - 01:13 PM.


#4 1wsx10

  • Members
  • 14 posts
  • LocationMelbourne, Australia

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 by 1wsx10, 05 February 2016 - 02:04 PM.


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 February 2016 - 06:24 AM

View Post1wsx10, on 05 February 2016 - 01:25 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users