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

[Retracted] Syntax question - are these two things 'the same' ? [No they aren't]

Started by Dog, 05 April 2014 - 10:08 PM
Dog #1
Posted 06 April 2014 - 12:08 AM
I'm trying to learn new and better ways of doing things so I recently tried replacing

if newSettings.outputType == "A" or newSettings.outputType == "D" then
with

if newSettings.outputType == ("A" or "D") then

It seems to work but before I start relying on this new 'technique' my question remains: are these two functionally equivalent and can I rely on either to produce the same results?

EDIT:
After further experimentation I've found that they aren't the same and I am still somewhat boggled at the many possible uses of 'and' and 'or'.
Edited on 05 April 2014 - 10:18 PM
CometWolf #2
Posted 06 April 2014 - 12:23 AM
They are indeed not the same.


if newSettings.outputType == ("A" or "D") then
is the same as

if newSettings.outputType == ("A")
Since or simply checks if it's a valid value(not nil or false). If it is, the subsequent values are discarded.
Dog #3
Posted 06 April 2014 - 12:30 AM
Thanks, CometWolf :)/> If you're so inclined, is there a 'better' or even another way to do this?

if newSettings.outputType == "A" or newSettings.outputType == "D" then
Edited on 05 April 2014 - 10:35 PM
Agoldfish #4
Posted 06 April 2014 - 12:46 AM
Thanks, CometWolf :)/> If you're so inclined, is there a 'better' or even another way to do this?

if newSettings.outputType == "A" or newSettings.outputType == "D" then
Thanks, CometWolf :)/> If you're so inclined, is there a 'better' or even another way to do this?

if newSettings.outputType == "A" or newSettings.outputType == "D" then
No, I don't think there is. That's a pretty good way of doing it.
CometWolf #5
Posted 06 April 2014 - 12:58 AM
When it comes down to so few variables checks, not really. An alternative when you have to deal with a lot, is to use tables.

local valid = {
  A = true,
  D = true
}
if valid[newSettings.outputType] then
Dog #6
Posted 06 April 2014 - 02:08 AM
When it comes down to so few variables checks, not really. An alternative when you have to deal with a lot, is to use tables.

local valid = {
  A = true,
  D = true
}
if valid[newSettings.outputType] then
I had a sneaky feeling tables might be a way of doing it, but I couldn't see how to do it effectively - thanks for the example :)/>
Bomb Bloke #7
Posted 06 April 2014 - 02:16 AM
When checks start getting a bit complex, in some cases it pays to relegate them off into functions. For eg, say you want to know if a given key event represents one out of a number of keys, you might do:

local myEvent = {os.pullEvent()}

if myEvent[1] == "key" and (myEvent[2] == keys.up or myEvent[2] == keys.w or myEvent[2] == keys.space or myEvent[2] == keys.enter) then

… or, you might make a dedicated key-checking function:

local myEvent

local function pressedKey(...)
  if myEvent[1] ~= "key" then return false end

  local args = {...}
  for i=1,#args do if args[i] == myEvent[2] then return true end end
  return false
end

myEvent = {os.pullEvent()}

if pressedKey(keys.up,keys.w,keys.space,keys.enter) then

Of course, the benefit depends on how often you're going to make checks of that type.
Edited on 06 April 2014 - 12:17 AM
Dog #8
Posted 06 April 2014 - 02:33 AM
I boggle at the myriad ways things can be approached in Lua. I'm going to have to give these examples some play time. Thanks, Bomb Bloke, CometWolf, and Agoldfish!
Edited on 06 April 2014 - 12:35 AM