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

Compare a string to everything in a table?

Started by Jtpetch, 20 September 2016 - 03:46 AM
Jtpetch #1
Posted 20 September 2016 - 05:46 AM
So I'm trying to be able to handle different ways a user could type things in my code, and I'm stuck.
What I'm trying to do, right now, is if the user types "Bye", "Goodbye", "bye", or "goodbye", the loop will exit, otherwise it will continue to run.
I know I could do this with an if then with some and's, but I'm trying to work with a while statement.

So, what I (thought) I could do is just compare the user's input (from read()) to a table containing each variation of "bye".
Aaaand I'm not entirely sure how to do that.
This code works correctly, but only if the user types exactly "bye", as it's only comparing it to the first string inside the function "bye"

local input=tostring(read())
local byeVars={"bye","goodbye","Bye","Goodbye"}
while input~=tostring(byeVars[1]) do
	input=tostring(read())
end
print("Goodbye!")

How would I go about comparing a string to all the variables inside a table, with a while function?
Is that even possible?
I looked at the built-in "go" program for turtles, since it has a similar ability, but it seems to be laid out a bit differently, and I wasn't sure how it's actually using the information.
valithor #2
Posted 20 September 2016 - 05:54 AM
Two decent solutions come to mind:

local input=tostring(read())
local byeVars={"bye","goodbye","Bye","Goodbye"}
var = true
while var do
  input=tostring(read())
  for k,v in ipairs(byeVars) do --# looping through the table
    if v==input then --# checking each entry to see if its in the table
      var = false
    end
  end
end
print("Goodbye!")


local input=tostring(read())
--# declaring the table where the values we are looking for are the keys
local byeVars={["bye"]=true,["goodbye"]=true,["Bye"]=true,["Goodbye"]=true}
repeat --# similar to while, will run at least once no matter the condition
  input = tostring(read())
  --# do other stuff
until not byeVars[input]
print("Goodbye!")
Edited on 20 September 2016 - 05:43 AM
Jtpetch #3
Posted 20 September 2016 - 08:08 AM
Ah thanks Valithor!
That second one works, though I had to remove the quotes inside the table, and I varied it a bit to just not use an if.

local input=tostring(read())
local byeVars={bye=true,goodbye=true,Bye=true,Goodbye=true}
while not byeVars[input] do
print("Hi!")
input=tostring(read())
end
print("Goodbye!")
Bomb Bloke #4
Posted 20 September 2016 - 12:58 PM
I'd be inclined to just do something like:

if read():lower():find("bye") then print("Yeah bye") end

… on the basis that all your example terms contain "bye" and you're unlikely to encounter a different command which also contains it.

Likewise, when expecting yes / no confirmation, I typically just convert the input to lowercase then check whether the first character is a "y" or an "n".

That second one works, though I had to remove the quotes inside the table, …

… because you also decided to remove the square brackets for whatever reason. If you decide to use terms that include special characters such as spaces, you'll want to do things the way valithor showed you.
valithor #5
Posted 20 September 2016 - 07:52 PM
-snip

… because you also decided to remove the square brackets for whatever reason. If you decide to use terms that include special characters such as spaces, you'll want to do things the way valithor showed you.

When i had originally posted the solution I had left out the square brackets. I went back around 30 minutes later and saw that I had forgotten them, chances are he saw it before it fixed it (Also removed the if he was referring to).
Edited on 20 September 2016 - 05:53 PM
Jtpetch #6
Posted 21 September 2016 - 06:12 AM
-snip

… because you also decided to remove the square brackets for whatever reason. If you decide to use terms that include special characters such as spaces, you'll want to do things the way valithor showed you.

When i had originally posted the solution I had left out the square brackets. I went back around 30 minutes later and saw that I had forgotten them, chances are he saw it before it fixed it (Also removed the if he was referring to).

Yes, they weren't there at the time.
Thanks for that info, though, I'm switching to the brackets, in case I do use special characters with this in the future.