lua-users home
lua-l archive

help with splitting strings

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm using this function from the lua users wiki:
function Split(str, delim, maxNb) -- from: http://lua-users.org/wiki/StringRecipes
 -- Eliminate bad cases...
 if string.find(str, delim) == nil then
 return { str }
 end
 if maxNb == nil or maxNb < 1 then
 maxNb = 0 -- No limit
 end
 local result = {}
 local pat = "(.-)" .. delim .. "()"
 local nb = 0
 local lastPos
 for part, pos in string.gfind(str, pat) do
 nb = nb + 1
 result[nb] = part
 lastPos = pos
 if nb == maxNb then break end
 end
 -- Handle the last field
 if nb ~= maxNb then
 result[nb + 1] = string.sub(str, lastPos)
 end
 return result
end
If I do this:
args = Split("byval filename as zstring ptr, byval force_channels as integer, byval reuse_texture_ID as uinteger, byval flags as uinteger", ",") then it works as expected and splits the string at the commas and returns a table with the segments. However, if I try to do it again like this:
words = Split(args[1], " ")
or
words = Split(args[1], "%s")
then what I get is a table with each element set to nil

AltStyle によって変換されたページ (->オリジナル) /