blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 15 Dec 2011 06:25 PM |
I want to know how to use ternary operators...how would I condense this?
if (m == 1) then x = "Pass" else x = "Fail" end |
|
|
| Report Abuse |
|
|
|
| 15 Dec 2011 06:27 PM |
x = m == 1 and "Pass" or "Fail"
I think.
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
| |
|
|
| 15 Dec 2011 06:29 PM |
yeah its what swim did except like this:
x=(m==1 and "Pass" or "Fail") |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
| |
|
|
| 15 Dec 2011 06:31 PM |
The only bad thing is this:
local rightAnswer = answer == 1 and false or true
This will be true every time. |
|
|
| Report Abuse |
|
|
|
| 15 Dec 2011 06:31 PM |
| yeah. he did it right. i was just suggesting the use of parantheses so its more organized. |
|
|
| Report Abuse |
|
|
|
| 15 Dec 2011 06:38 PM |
Ternary is faster than if ... else. And it's more fun to think about :D
local x = 3 local y = "3" local z = {3} local a = (x == tonumber(y) and tonumber(y) == z[1] and x == z[1] and z or x == tonumber(y) and y or x) print(type(a) .. ": " .. tostring(a)) --> table: table: [hexadecimal] |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 15 Dec 2011 07:56 PM |
It isn't actually ternary operators, it's binary operators that are being used to mimic
x=m==1 ? 1 : 0 |
|
|
| Report Abuse |
|
|