Jump to content




Splitting A String In Lua


  • You cannot reply to this topic
3 replies to this topic

#1 NullSchritt

  • Members
  • 43 posts

Posted 22 July 2013 - 01: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

Quote

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.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 July 2013 - 01:51 PM

num, command, arg = string.match(commandString, "(%d+)|(%a+)|(%a+)")

#3 oedze

  • Members
  • 40 posts

Posted 22 July 2013 - 02:10 PM

View PostLyqyd, on 22 July 2013 - 01:51 PM, said:

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

#4 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 23 July 2013 - 12: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 |.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users