Hi
I have a string from database that I need to parse into diffeent text objects. For example:
“ingrid|2 eggs;<br />butter;<br />2 bananas; ||steps|Prepare bananas!<br />Make a pie.||complexity|average”
I need to separate the words before the patterns (ingrid|, <br />, ||steps| etc.) and be able to parse them to different text objects. How can I do this?
You can google “lua split string”. Then you will find something like this function:
[lua]local function split(str, pat)
local t = {} – NOTE: use {n = 0} in Lua-5.0
local fpat = “(.-)” … pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= “” then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
local test = “ingrid|2 eggs;<br />butter;<br />2 bananas; ||steps|Prepare bananas!<br />Make a pie.||complexity|average”
local result = split(test, “<br />”)
for i = 1, #result do
print(result[i])
end[/lua]
Well, it depends how you want to split things up, but using match() is the simplest. It may be worth hunting round, though, there may be a lua library already which will parse HTML or possibly an XML one which will do. It really depends how complex your needs are - if it is basically always parsing what you show above, you can just match it.
There are dozens of string splitting functions that you can find at places like the Lua String recipes site. My personal favorite is:
function string:split( inSplitPattern, outResults ) if not outResults then outResults = { } end local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end
Rob
Okay, thank you. I’ve googled it, but didn’t find how to make it separate values.
Yet another quick question: in my database I have category column and some rows are in several categories “10,21,28,42,22,48,49,50”.
when I use "SELECT * FROM table name WHERE category = ‘10’ " it returns rows only with one number 10. If the rows have several categories listed it won’t use it. How it is possible to make it select the row if the number needed is listed?
I though about using match() and comma as a pattern but I don’t understand how to pass it correctly to sql command. Any ideas?
You can use wildcards like this: “SELECT * FROM table name WHERE category like ‘%10%’”. But if you are selecting category 1 you will get category 10 also etc.
So I guess you will have to make something ugly like: “SELECT * FROM table name WHERE category like ‘%,10,%’ or category like '%,10” or category like '10,%" or category like '10"
To it will cover 10 in the middle, 10 at the end, 10 at beginning and only 10. Maybe there are better options, but I don’t think so in SQLite.
If you can you should redesign your database to be relational. So that you would have a separate categories table that you can join to your other table.
I see, thank you! will try to implement this.
You can google “lua split string”. Then you will find something like this function:
[lua]local function split(str, pat)
local t = {} – NOTE: use {n = 0} in Lua-5.0
local fpat = “(.-)” … pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= “” then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
local test = “ingrid|2 eggs;<br />butter;<br />2 bananas; ||steps|Prepare bananas!<br />Make a pie.||complexity|average”
local result = split(test, “<br />”)
for i = 1, #result do
print(result[i])
end[/lua]
Well, it depends how you want to split things up, but using match() is the simplest. It may be worth hunting round, though, there may be a lua library already which will parse HTML or possibly an XML one which will do. It really depends how complex your needs are - if it is basically always parsing what you show above, you can just match it.
There are dozens of string splitting functions that you can find at places like the Lua String recipes site. My personal favorite is:
function string:split( inSplitPattern, outResults ) if not outResults then outResults = { } end local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end
Rob
Okay, thank you. I’ve googled it, but didn’t find how to make it separate values.
Yet another quick question: in my database I have category column and some rows are in several categories “10,21,28,42,22,48,49,50”.
when I use "SELECT * FROM table name WHERE category = ‘10’ " it returns rows only with one number 10. If the rows have several categories listed it won’t use it. How it is possible to make it select the row if the number needed is listed?
I though about using match() and comma as a pattern but I don’t understand how to pass it correctly to sql command. Any ideas?
You can use wildcards like this: “SELECT * FROM table name WHERE category like ‘%10%’”. But if you are selecting category 1 you will get category 10 also etc.
So I guess you will have to make something ugly like: “SELECT * FROM table name WHERE category like ‘%,10,%’ or category like '%,10” or category like '10,%" or category like '10"
To it will cover 10 in the middle, 10 at the end, 10 at beginning and only 10. Maybe there are better options, but I don’t think so in SQLite.
If you can you should redesign your database to be relational. So that you would have a separate categories table that you can join to your other table.
I see, thank you! will try to implement this.