Wednesday, December 12, 2012

lua string to table

I couldn't find a clear way to do this... the entire bullshit that everyone always does is just reference the damn wiki. Which is about as clear as mud. I hate it when programmers assume you're already a programmer.

in anycase given a string like
myString = "this is a long string"
and you want to turn this into an array of letters
like myArray = {t,h,i,s, ,i,s, , a, ,l,o,n,g, ,s,t,r,i,n,g}
for reasons of being able to do things based on each letter.

you should do this:

myArray = {} -- make empty array
for i = 0, #myString do -- #myString is the length of the string
    local s = string.sub(myString,i,i) -- gets the character at location i in the string
   myArray[i] = s -- assigns the table[i] to the character which was returned to s
end


that's it... a clear example with, imagine that!

rxokita's shared items