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

[Question] "else" and "or"

Started by spaites, 12 October 2012 - 11:25 PM
spaites #1
Posted 13 October 2012 - 01:25 AM
Here's my code (snipit of it):

if input == "spaites" or "iamtk421" then
  sleep(2)
  term.clear()
  term.setCursorPos(1, 1)
  print ("Techonauts")
  print ("^^^^^^^^^^^")
  print ("")
  print (""..input.."")
  else
  print ("Go away!")
  sleep(2)
  term.clear()
  term.setCursorPos(1, 1)
end
The problem is that when i type somthing different from "spaites" or "iamtk421" it still goes through that part of the code not doing going to the "else" part.
Please help.
JoshhT #2
Posted 13 October 2012 - 01:28 AM
Change that if statement to,


if input == ("name1" or "name2") then
  --Stuff
else
  --Fuck off
end

The longer version of what you want to accomplish would be this.


if input == "name1" or input == "name2" then
Lyqyd #3
Posted 13 October 2012 - 01:30 AM
This line is the problem in the logic flow:


if input == "spaites" or "iamtk421" then

You think that this means, "if input is equal to either 'spaites' or 'iamtk421' then", when what it really means is, "if input is equal to 'spaites' or if 'iamtk421' is not nil and is not false then". Change that line to:


if input == "spaites" or input == "iamtk421" then

Edit to respond to JoshhT's post written while I was writing this reply:


Change that if statement to,


if input == ("name1" or "name2") then
  --Stuff
else
  --Fuck off
end

The longer version of what you want to accomplish would be this.


if input == "name1" or input == "name2" then

That code is also incorrect. Your suggested statement now is effectively, "if input == 'name1' then", since the result of ("name1" or "name2") would always be the first non-nil, non-false entry.
faubiguy #4
Posted 13 October 2012 - 01:32 AM
Change that if statement to,


if input == ("name1" or "name2") then
  --Stuff
else
  --Fuck off
end

The longer version of what you want to accomplish would be this.


if input == "name1" or input == "name2" then

That won't do anything if input is "name2" because ("name1" or "name2") is equal to "name1". What you want is

if input == "name1" or input == "name2" then
  -- Entered correct name
else
  -- Entered incorrect name
end
JoshhT #5
Posted 13 October 2012 - 01:35 AM
Lua sucks, that would have worked in Java…
MysticT #6
Posted 13 October 2012 - 03:23 AM
Lua sucks, that would have worked in Java…
No it wouldn't. 2 reasons: or (|| in java) is used with booleans, not strings; strings are compared with String.equals, not ==
spaites #7
Posted 13 October 2012 - 03:26 AM
I have a problem, i want to do this (this is the incorrect name part):

else
  print ("Go Away!")
  term.clear()
  term.setCursorPos(1, 1)
  os.reboot() -- part where it wont work
end
I'm using the same post cause it's the same code.
Lyqyd #8
Posted 13 October 2012 - 06:09 AM
What doesn't work about os.reboot()?
TheOnlyExedra #9
Posted 13 October 2012 - 10:56 AM
I'm not sure, but I think this'll work:

local tab = {"spaites","iamtk421"}
local input = io.read()
for i, v in pairs(tab) do
if
string.lower(input) == string.lower(v) then –[]
sleep(2)
term.clear()
term.setCursorPos(1,1)
print([[
Technonauts
^^^^^^^^^^^
]])
print(""..input.."")
elseif string.lower(input) ~= string.lower(v) then –not sure if table continues here
print ("Go away!")
sleep(2)
term.clear()
term.setCursorPos(1,1)
end
end
JoshhT #10
Posted 13 October 2012 - 02:53 PM
Lua sucks, that would have worked in Java…
No it wouldn't. 2 reasons: or (|| in java) is used with booleans, not strings; strings are compared with String.equals, not ==

Are you retarded, or just think I'm stupid or something?
A method to return a boolean value isn't difficult nor problematic.
Orwell #11
Posted 13 October 2012 - 03:19 PM
Lua sucks, that would have worked in Java…
No it wouldn't. 2 reasons: or (|| in java) is used with booleans, not strings; strings are compared with String.equals, not ==

Are you retarded, or just think I'm stupid or something?
A method to return a boolean value isn't difficult nor problematic.

If you now that, then why do you say that it would've worked in java? No reason to call names…
Doyle3694 #12
Posted 13 October 2012 - 03:56 PM
plus, this is CC forums using lua. If you think lua suck and java is awesome then find some java forum.

You're like all those youtube guys going to watch some guys videos just to say they suck. just stfu, ragequit cc and get a life.
MysticT #13
Posted 13 October 2012 - 09:38 PM
Lua sucks, that would have worked in Java…
No it wouldn't. 2 reasons: or (|| in java) is used with booleans, not strings; strings are compared with String.equals, not ==

Are you retarded, or just think I'm stupid or something?
A method to return a boolean value isn't difficult nor problematic.
Now, you said that this code:

if input == ("name1" or "name2") then
would have worked on java. If you really know java, you'd know that you can't use or with something that is not a boolean. Also, comparing strings is done with String.equals.
This would be the correct way to do it (in java):

if (input.equals("name1") || input.equals("name2"))
wich is almost the same that in lua.
Also, that has nothing to do with "A method to return a boolean value", so I'm not sure why you said that.
So yeah, I'll choose the second option.
KaoS #14
Posted 13 October 2012 - 10:05 PM
Lua sucks, that would have worked in Java…
No it wouldn't. 2 reasons: or (|| in java) is used with booleans, not strings; strings are compared with String.equals, not ==

Are you retarded, or just think I'm stupid or something?
A method to return a boolean value isn't difficult nor problematic.

whoah dude, calm down there… insulting people on forums is not going to help anything (especially when you're insulting this guy :)/>/> MysticT~='retard'; MysticT=='genius')

If you are in the right then that's great but try to avoid throwing derogatory names around. much appreciated
PixelToast #15
Posted 13 October 2012 - 10:20 PM
so, your raging about the differences in compare expressions
its only a single line of code
JoshhT #16
Posted 14 October 2012 - 01:09 AM
Setting things straight.

I was not at all raging, I'm Australian, that is normal talk for me, I apologize to anybody who was offended.
(I call my best friend cunt like fifty times a day)

MysticT did slightly push my buttons earlier, I do not appreciate it when people just assume things.
When I said, "That would have worked in Java."
Obviously I did not mean the exact code,

if x == ("This" or "That")

I would have done something close to what Mystic posted up earlier, but to show I'm absolutely not copying code,
And for example, I'm comparing strings a lot,

public boolean matches(s, t) {
  return s.equalsIgnoreCase(t) ? false : true;
}
MysticT #17
Posted 14 October 2012 - 01:37 AM
Setting things straight.

I was not at all raging, I'm Australian, that is normal talk for me, I apologize to anybody who was offended.
(I call my best friend cunt like fifty times a day)

MysticT did slightly push my buttons earlier, I do not appreciate it when people just assume things.
When I said, "That would have worked in Java."
Obviously I did not mean the exact code,

if x == ("This" or "That")

I would have done something close to what Mystic posted up earlier, but to show I'm absolutely not copying code,
And for example, I'm comparing strings a lot,

public boolean matches(s, t) {
  return s.equalsIgnoreCase(t) ? false : true;
}
Well, you didn't say that :)/>/>.
Also, that's a different situation. The lua equivalent of that is:

function matches(s, t)
  return s == t and true or false
end
I know it's easier with "return s == t", it's just as an example.
Comparing a variable against multiple values is done the same way in lua and java. So no need to say that lua sucks :)/>/>.
Anyway, there's nothing more to say here. Sorry if I offended you in any way.