Monday, April 16, 2012

lua split?

How would I split a string by a parameter?

Example

mystring = "a b c d"

var = split(mystring," ")

So var(0) would be a, 1 would be b, etc.|||Hi,

Blizz provide a function for doing this called 'strsplit'.

strsplit returns a list of values, and not a table

So :


Code:
mystring = "a b c d"
var1, var2, var3, var4 = strsplit(" ", mystring)

But you can use a nifty trick with curly brackets to place these values in a table automatically :


Code:
mystring = "a b c d"
var = { strsplit(" ", mystring) }
print( var[1] )
|||You want strsplit. See the example for how to get a table back from it (just wrap the return in a table declaration).

EDIT: Telic beat me, had this window open for ages before replying :) .

No comments:

Post a Comment