Trying to turn what I get back from the who list in matches[2] into a table.
The list looks like this: Chris, David, Edward and Frank.
However, when I run it through the code I get something like:
Chris
D
vi
E
w
r
Fr
k
Thoughts?
Code I am using for testing now:
delim = {",", "and"}
s = matches[2]
p = "[^"..table.concat(delim).."]+"
for w in s:gmatch(p) do
echo("\n" .. w)
end
Help converting string into seperate strings
Re: Help converting string into seperate strings
For those looking for a solution to this same issue. This is how I got it to work:
delim = {",", "."}
s = matches[2]:gsub(" and ", ", ")
s = s:gsub(", ", ",")
p = "[^" .. table.concat(delim) .. "]+"
for w in s:gmatch(p) do
echo("\n" .. w)
end
delim = {",", "."}
s = matches[2]:gsub(" and ", ", ")
s = s:gsub(", ", ",")
p = "[^" .. table.concat(delim) .. "]+"
for w in s:gmatch(p) do
echo("\n" .. w)
end
Re: Help converting string into seperate strings
Since this seems like common pattern I figured an extra solution wouldn't hurt. This is the simplest code I could come up with:
Code: Select all
local str = 'Chris, David and Edward.'
local names = {}
for word in str:gmatch("(%w+)") do
if word ~= 'and' then table.insert(names, word) end
end
display(names)
Re: Help converting string into seperate strings
You can also do
Re: Help converting string into seperate strings
Ooh, readable and even shorter, I like that one!