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

How To ... Split String In Parts To Compare?

Started by Neekow, 07 November 2013 - 08:39 AM
Neekow #1
Posted 07 November 2013 - 09:39 AM
Hi every one, i'm back (maybe some people remember me with my unfinished NUP … think i'll do all comments if ever someone want to finish it).

I'm on a new project, and as every time, i wanna use function that i dunno how to do (i love challenge). My project: "Box!" Using a wireless_modem/chat_box turtle, wanna do an sarcastic assistant ^^. But after a day of reshearch i didnt find how to split a string in many parts.

What i wanna do:
when i say: "@Box! Ca roule?" (trans:"@Box! What's up?"). I want to split the string in 2 parts:
"@Box!" and "Ca roule?".
why? to avoid spam from Box! to be able to give many differents orders (like @Box.maths @Box.wifi @Box.goTroll etc …)

But … after a huge amount of try, i've to admit that i really don't know how to do that.

i tried (not my only try, but they were worst than this one):
local message = {tostring(message)}
    for i =  #message , 4 do
	    ID = ID..tostring(message[i])
	    print(" "..ID)
    end  

but for i = 1, it will return message, not the 1st letter =$

anyone help? =)
Engineer #2
Posted 07 November 2013 - 09:47 AM
To return a letter at a certain character, you can use the function string.sub( ... ) (scroll down a little bit though).

But to split a string like that, you need a point where it splits. In your case it would be the exclamation mark, I guess. For that you can use your logic and do something like this:

local function parse( message )
  -- Check if message is a string on this line

  for i = 1, #message do
     if string.sub( message, i, i ) == "!" then
       return string.sub( message, 1, i ), string.sub( message, i + 1 )
     end
  end
  error( "No exlamation mark found D:", 2 )
end
Neekow #3
Posted 07 November 2013 - 11:17 AM
Hum, will try now ^^

[EDIT]: Yeah, it's going in the right way but it's not finished yet x)

i did some modifications

box = peripheral.wrap("left")
while true do
  event, player, message = os.pullEvent("chat")
  print(message)
    if string.find(message,"!Box") then
	  print("bla")
	    if string.find(message,".maths") then
		  print("maths")
	    end
    end
end

With that, it will recognize when i say "!Box" or "!Box.maths" but not ".maths" so everything is good ^^

(yeah, if i understood well, string.sub is to find a character, string.find is used to find a word?)
Edited on 07 November 2013 - 10:42 AM
Neekow #4
Posted 07 November 2013 - 06:10 PM
after reading and understanding the link you gave me (yeah, english isn't my native langage)

i think i'll do something like


local orders = {
  {" ", <function>},
  {".maths", <function>}}

for i, order in ipairs(orders) do
   if string.sub(message,1,nameLen+string.len(order[1])) == "!"..name..order[1] then etc .... 

by this way, to speak to Box, i'll have to do a sentence like:
"!Box.goTroll ……."
to be listened, something like:
"blablablabla, !Box …."
won't work