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

A way to search for keywords in a read()?

Started by CCJJSax, 03 February 2013 - 09:26 AM
CCJJSax #1
Posted 03 February 2013 - 10:26 AM
I'm doing a help program for a remote control turtle program that does about 35 things with lots of features using key presses Right now I'm writing a separate program for it that acts like a help function where a player can type in a button from the movement programs display to tell them what it does, or have them type in what they want it to do, and have it return how to do that with detail. Currently I have it set up as a local function set to each thing (that I have written this far) and have a ton of elseif s to match alternatives to what the player might ask, including capitalization differences. I thought of converting to lower case, but the key presses use capitals and lowercase, so that wouldn't work, and elseifs are easier to navigate than lots of or s in Notepad.

so here is my thought, is there a way that I can have it search your input for specific words? So If they wanted to go down, and stated it like "how do I make the turtle go down?" it would see the word "down" and refer to a local function that describes how to do that?

I am really hoping for something like this because using elseifs have made my code over 700 lines with only about 10% of the buttons covered.
remiX #2
Posted 03 February 2013 - 10:34 AM
Use string.find(variable, "text") to see if "text" lies within variable


var = "How do I make the turtle go down?"

if string.find(var, "down") then
    -- found the word 'down' in the variable
end

-- Or you can do it like this:

if var:find("down" then

end
Kingdaro #3
Posted 03 February 2013 - 10:35 AM
string.match helps a lot here. For example, if I wanted to see if the word "spaghetti" is in a string:


local somestr = "I like spaghetti!"

if somestr:match("spaghetti") then
  print 'Nice!'
else
  print 'Oh.'
end

string.find works too.
theoriginalbit #4
Posted 03 February 2013 - 10:37 AM
Something like this would work:

local function helpDown() 
end
local function helpUp() 
end
-- etc

local tCommands = {
  ["down"] = helpDown, -- left side is the command
  ["up"] = helpUp, -- right side the function name to call web found
  -- etc
}
input = read()

for key, value in pairs(tCommands) do
  local found = input:find(key)
  if found then
    value() -- call our stored function
    break -- exit our loop, we found what we needed
  end
end

EDIT: see here for more info on string functions http://lua-users.org/wiki/StringLibraryTutorial
CCJJSax #5
Posted 03 February 2013 - 11:34 AM
Use string.find(variable, "text") to see if "text" lies within variable


var = "How do I make the turtle go down?"

if string.find(var, "down") then
	-- found the word 'down' in the variable
end

-- Or you can do it like this:

if var:find("down" then

end

Dude! You just saved me a lot of time and a lot of code!!! :D/> the other alternatives that was posted probably worked, yours just seemed the easiest. thank you all soooo much!!!!!
theoriginalbit #6
Posted 03 February 2013 - 11:54 AM
if var:find("down" then
TheOriginalBIT:1 [string "remiX help"]:9:Expected ')' found 'then' :P/>
remiX #7
Posted 03 February 2013 - 11:57 AM
>< I saw the error, but was too lazy to edit it xD And then indentation screws up and blah! I'll leave it there to annoy you :P/>