41 posts
Posted 06 March 2014 - 02:28 PM
Looked around and can't seem to find anything on this. I think it might be easier than I figure it is, but I'm wanting to take what a user inputs and references it towards a table, then if the reference matches, do some shit. Here's my code in question. And yes I know what I put won't work, I was just trying different ways I could think of and that was the last thing I tried.
responseYes = { "y", "ye", "yes", "Y", "Ye", "Yes", "YE", "YEs", "YES" }
responseNo = { "n", "no", "N", "No", "NO" }
serverPort = ""
local function startup()
shell.run("clear")
print("Initiating Startup Procedure...")
-- Rednet Open Ports Probing
local modemSide
for _,side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "modem" then
modemSide = side
print("Modem Detected on the "..modemSide..".")
print("Is this the correct Modem? \n")
userInput = io.read()
if userInput == responseYes then
print("derp")
elseif userInput == responseNo then
print("herp")
else
print("failed")
end
end
end
end
startup()
110 posts
Posted 06 March 2014 - 02:37 PM
What you're trying to do is similar to what can be done in other languages, such as C#:
if (responseYes.Contains(userInput))
//herp
else if (responseNo.Contains(userInput))
//derp
else
//failed
Unfortunately, Lua does not have a Contains() function; you'll have to write that yourself, iterating through every item in the table and returning a Boolean. I recommend it; it's a handy function I use often.
41 posts
Posted 06 March 2014 - 02:42 PM
Bah, if that's the case then I guess I'll have to figure out how to do that then. My programming is scratchy in some areas as I haven't learned by the book. It'll take some research I suppose.
1281 posts
Posted 06 March 2014 - 02:42 PM
There is a much easier way of doing it, much less resource intensive aswell
local tTable = {}
local input = read()
if tTable[input] then
--user input matched a table entry
else
--user input did not match any table entry
end
7508 posts
Location
Australia
Posted 06 March 2014 - 02:46 PM
to expand on what CometWolf said you can make your job even easier by using
string.lower which makes a provided string lowercase; this means you need less values in the tables.
Working code example (CometWolf was missing some important stuff in the table creation for you)
local yes = {["yes"] = true; ["y"] = true; ["yep"] = true; ["yeah"] = true; ["yeh"] = true;}
local no = {["no"] = true; ["n"] = true; ["nope"] = true; ["nah"] = true}
local input = string.lower(read())
if yes[input] then
--# if yes
elseif no[input] then
--# if no
else
--# invalid
end
Edited on 06 March 2014 - 01:47 PM
41 posts
Posted 06 March 2014 - 02:54 PM
Awesome, that works like a charm.
My new working code:
local responseYes = { ["yes"] = true; ["y"] = true; ["yep"] = true; ["yeah"] = true; ["yeh"] = true; }
local responseNo = { ["no"] = true; ["n"] = true; ["nope"] = true; ["niet"] = true; }
serverPort = ""
local function startup()
shell.run("clear")
print("Initiating Startup Procedure...")
-- Rednet Open Ports Probing
local modemSide
for _,side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "modem" then
modemSide = side
print("Modem Detected on the "..modemSide..".")
print("Is this the correct Modem? \n")
local userInput = string.lower(read())
if responseYes[userInput] then
print("derp")
elseif responseNo[userInput] then
print("herp")
else
print("failed")
end
end
end
end
startup()
Now I can continue on with it. Thank you. :D/>
7083 posts
Location
Tasmania (AU)
Posted 06 March 2014 - 03:49 PM
Another trick if you
really want to trim things down; if the user input started with a "y", take it as a positive response, or if it started with an "n" treat it as negative.
local input = string.lower(string.sub(read(),1,1))
if input == "y" then
--# if yes
elseif input == "n" then
--# if no
else
--# invalid
end
Somewhat cheating in that there may be words out there that start with "y" that're negative, or words that start with "n" that're positive. I've never been able to think of any though, and doubt they'd see common enough usage to worry about. ;)/>
Either way, definitely read through
this for more fun with string manipulation.