lines_from(file) - It's a bit strange that this function accept the file handle. It would make more sense to give it the file name and have the function open the file.
function lines_from(filename)
local file = io.open(filename, "r")
if not file then return {} end
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
file:close()
return lines
end
function table_find_word(table, word)
for key,value in ipairs(table) do
if string.find(value,word,1,true) do
return true
end
end
return false
end
Note: I added "1,true" to string.find so it disables the pattern matching which could cause problems with words that have special characters in them such as '-'. Generally if you are not using pattern matching you should disable it to make sure it doesn't inadvertently cause problems in the future.
I hope you find that useful.
Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
Thanks for the feedback. I actually didn't know that those functions already existed, guess I should have read the wiki a little closer...
The code you wrote for lines_from is good also, the function I originally wrote calls the file_exists function so it's practically the same functionally.
table_find_word explodes a table into strings (which I am now seeing there is a table explode function in the wiki!) and then searches those for the word specified.
Finding a whole string would be table_contains_element.
So basically, what I'm getting from this is that I put a bunch of unnecessary work into my project...well, at least it was a LUA learning experience!