Combrad
|
  |
| Joined: 18 Jul 2009 |
| Total Posts: 11025 |
|
|
| 30 Oct 2011 09:46 AM |
So, that would convert:
msg2 = "Ohai;How;are;you;?" Divider = ";" loadstring('ohr = {"'..string.gsub(msg2,Divider,'","')..'"}')()
to:
{"Ohai","How","are","you","?"}
I want to do it with a loop. |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2011 10:31 AM |
local function parse(input) local output = {}; -- the table we will store everything in input = input .. ";" -- add another separator, will make parsing super easy
for str in input:gmatch("(.-);") do -- look for patterns, matching any characters between a semicolon output[#output + 1] = str; -- add the string to the output end
return output; -- return end
local tab = parse("Ohai;how;are;you;?");
for i = 1, #tab do print(i, tab[i]); end
>1 Ohai >2 how >3 are >4 you >5 ? |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2011 10:33 AM |
msg2 = "Ohai;How;are;you;?" Divider = ";" function explode(str,div,ignorespaces) if div=='' then return end local tab = {} local cur = "" for i = 1, string.len(str) do stat1 = stat1 + 1 -- See what i mean? if str:sub(i,i) ~= enter and (ignorespaces == true or str:sub(i,i) ~= " ") then if str:sub(i,i) == div then tab[#tab+1] = cur cur = "" else cur = cur .. str:sub(i,i) end end end if cur ~= "" then tab[#tab+1] = cur cur = "" end return tab end
local t = explode(msg2, Divider, true)
-- efficiency. Loadstring is BAD BAD BAD. do not use it to get a variable or you will regret it later. |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2011 10:34 AM |
And if you want it to ignore enters, put this at the start of the script.
local enter = ([[l l]]):sub(2, 2) |
|
|
| Report Abuse |
|
|