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

How do I split strings up?

Started by Sammich Lord, 19 August 2012 - 08:02 AM
Sammich Lord #1
Posted 19 August 2012 - 10:02 AM
How would I split up a string? For instance I want to have a program that checks which words are types for instance: lights on.
Or something pretty basic like that, I know the obvious answer is to make a lights program then use args to turn on/off but I basically want to have a program to control the whole system (add users, del users, perms) and stuff like that.
BigSHinyToys #2
Posted 19 August 2012 - 10:14 AM
no read() cant split up a string
string.match() can
string.gmath() can
string.sub() can

But it depends on what you are trying to do
http://www.lua.org/manual/5.1/manual.html#pdf-string.sub
Pharap #3
Posted 19 August 2012 - 12:09 PM
How would I split up a string? For instance I want to have a program that checks which words are types for instance: lights on.
Or something pretty basic like that, I know the obvious answer is to make a lights program then use args to turn on/off but I basically want to have a program to control the whole system (add users, del users, perms) and stuff like that.

I'd recommend doing something like:

local input = read()
if string.sub(input,1,3) = "Del" then
if string.sub(input,5,9) = "User" then
local user = string.sub(input,10)
DeleteUser(user)
end
end
end
And basically follow that pattern for all commands, not forgetting to account for the spaces between words.
You could also do it by breaking up the words with commas when writing and then in the code creating separate strings for the words between the commas.
Lyqyd #4
Posted 19 August 2012 - 04:20 PM
Take a look at how the shell splits up command arguments to send to the programs it's running. That's how you'll want to do this.