LUA : Method to split a string and output it into a array

First of all i need to give credit to www.wellho.net. I got some code from their website also to implement this.
My basic requirement was to read from the input arguments and display it as a vector of type integers.
Below is the code. Pardon me for the formatting issues. WordPress sucks when coding:
function string:split(delimiter)
local result = { }
local from  = 1
local delim_from, delim_to = string.find( self, delimiter, from  )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from  = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from  )
end
table.insert( result, string.sub( self, from  ) )
return result
end
Then I call the below code from my main method:
have = string.split(inputArgs, “,” )
names= {};
for index,today in pairs(have) do
if today == “1” then
names[index] = 1;
elseif today == “2” then
names[index] = 2;
elseif today == “4” then
names[index] = 4;
end
end
return names; //this returns the vector of integers

Leave a Reply

Your email address will not be published. Required fields are marked *