Page 1 of 1
LUA Table & File manipulation addon
Posted: Fri Mar 29, 2013 3:29 pm
by attackspeedzero
I started piling up various functions for file, table, and string manipulation, so I thought I'd share them with you all.
Current list of functions:
- file_exists(file)
- Checks to see if a file exists
- returns true or false
- lines_from(file)
- Creates a table from all lines in a file
- returns table
- table_contains_element(table, element)
- Checks to see if a table contains the specified element
- returns true or false
- table_find_word(table, word)
- Breaks down every element of a table into space-delimited strings, then checks all strings for the specified word
- returns true or false
Now part of
addon_attackspeedzero. Link removed from this thread.
Re: LUA Table & File manipulation addon
Posted: Sat Mar 30, 2013 1:43 am
by rock5
file_exists(file) - Or you could use
fileExists(file).
http://www.solarstrike.net/wiki/index.p ... fileExists
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.
Code: Select all
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
table_contains_element(table, element) - Or you could use
table.contains(tab, value).
http://www.solarstrike.net/wiki/index.p ... e.contains That's not a regular lua function. It's one added by Administrator.
Why not just do a straight string match? It would make it a more versatile function. Why limit it to only single words? Eg.
Code: Select all
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.
Re: LUA Table & File manipulation addon
Posted: Mon Apr 01, 2013 1:59 pm
by attackspeedzero
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!