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

Splitting A String In Lua

Started by NullSchritt, 22 July 2013 - 12:49 PM
NullSchritt #1
Posted 22 July 2013 - 02:49 PM
Hello, I am developing a system that allows computers to remotley access commandblocks in a central processing center(since players cannot use commandblock and they are required for the functionality of the app)

I have got it pretty much done, but I have one question.

On the "server" computers hooked up to the commandblocks, they will request a command to execute from a webpage, which will return a string as follows
1|SPAWN|TRENSALOR

I need to split this into 3 different variables, so that I can pass the last two to the commandblock, then send a request back to the server with the request ID(in this case "1"), to remove the request from the database.

I am 100% competent with PHP, but not so much with LUA, so, how would I go about splitting this string, into 3 individual variables?

Thanks for taking the time to read my help request.
Lyqyd #2
Posted 22 July 2013 - 02:51 PM
num, command, arg = string.match(commandString, "(%d+)|(%a+)|(%a+)")
oedze #3
Posted 22 July 2013 - 03:10 PM
num, command, arg = string.match(commandString, "(%d+)|(%a+)|(%a+)")
thanks with that one, always wanted to know how to do that, but can you give a explanation of how it works???, would be great :)/>

-oedze
Grim Reaper #4
Posted 23 July 2013 - 01:36 AM
Check out http://www.lua.org/pil/20.2.html

The (%d+) means that out of the match call, the first parameter returned is a number with any number of digits between 1 and however many digits until there is a character which isn't a digit. The following | means that there will be a | in the pattern after that number which is being matched.

The following (%a+) means the same as the number capture above, but with only letters. The following | means that there will be a | in the pattern after that word which is being matched. The following (%a+) is the same as above but without the ending |.