Amalgam
|
  |
| Joined: 08 Oct 2012 |
| Total Posts: 54 |
|
|
| 21 Oct 2012 06:51 PM |
'or' assigns the first non-nil value right?
like:
a = 29 b = nil c = 35
d = a or b or c
d would equal 29 right? then:
d = b or c or a
d would then equal 35?
I just want to know if this is really what it's doing. |
|
|
| Report Abuse |
|
|
|
| 21 Oct 2012 06:55 PM |
You mean ternary operators?
This is how they work:
variable = (condition) and (if condition is true, variable equals this) or (if condition is not true, variable is this)
local stuff = 1+2==3 and "That\s true!" or "That\s false!"
print(stuff)
> That's true!
local stuff = 1+2==4 and "That\s true!" or "That\s false!"
print(stuff)
> That's false! |
|
|
| Report Abuse |
|
|
Amalgam
|
  |
| Joined: 08 Oct 2012 |
| Total Posts: 54 |
|
|
| 21 Oct 2012 06:58 PM |
>>woodstauk4
Thanks. I was assignment with 'or' once in a code I found and wasn't sure how it worked. |
|
|
| Report Abuse |
|
|
Amalgam
|
  |
| Joined: 08 Oct 2012 |
| Total Posts: 54 |
|
|
| 21 Oct 2012 07:04 PM |
Okay so if my code is:
myValue = false toggle=function(v) v = v and false or true return v end print(myValue) toggle(myValue) print(myValue) toggle(myValue) print(myValue)
then the output would be: false true false
Is that right? |
|
|
| Report Abuse |
|
|
| |
|
Amalgam
|
  |
| Joined: 08 Oct 2012 |
| Total Posts: 54 |
|
|
| 21 Oct 2012 07:11 PM |
| Thanks for teaching me this. Very helpful |
|
|
| Report Abuse |
|
|