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

Seperating A String Into Multiple Variables

Started by Inumel, 18 November 2013 - 03:35 PM
Inumel #1
Posted 18 November 2013 - 04:35 PM
I am planning on making a Chat based teleport system with miscperipherals chatbox and a command block, so all the user has to say is "Teleport to playername" and it will teleport them to said player

I wish to get the playername they want to teleport out of that string, and need a bit of help doing it

I tried putting it into a table first, IE

a = "teleport to playername"
tab = {}
table.insert(tab,a)
print(tab[1],tab[2],tab[3])
Sadly this only put the entire string on tab[1](which i honestly expected)

So can anyone tell me how to seperate the string by word and insert it into a table?

thanks!
TheOddByte #2
Posted 18 November 2013 - 04:37 PM
Well you should look into string.gsub and patterns that is on the next page of the link I posted.
Inumel #3
Posted 18 November 2013 - 04:39 PM
Ill do that! thank you
Inumel #4
Posted 18 November 2013 - 04:49 PM
Extremely confusing! no wonder i havent messed with gsub before :s
Inumel #5
Posted 18 November 2013 - 05:25 PM
So yeah, after reading this page over twice i still cant figure it out.. can you perhaps give me an example? or the solution to my issue? i apologize if thats a problem but i simply cannot figure it out
Bomb Bloke #6
Posted 18 November 2013 - 05:51 PM
There are other ways, but I'd do:

a = "teleport to playername"
if a:sub(1,12):lower() == "teleport to " then
  print("I'm gonna teleport you to "..a:sub(13,#a))
end
Inumel #7
Posted 18 November 2013 - 05:58 PM
Works perfectly, edited for my specific purpos:

local evt, player, message = os.pullEvent("chat")
if message:sub(1,12):lower() == "teleport to " then
  print(message:sub(13,#message))
end
Thank you very much for the help, saved me a lot of hassle!