LUA Table & File manipulation addon

Additional botting resources. Addons may be either for the game itself or for the RoM bot.
Forum rules
Only post additional bot resources here. Please do not ask unrelated questions.
Post Reply
Message
Author
User avatar
attackspeedzero
Posts: 28
Joined: Sat Mar 02, 2013 6:24 pm

LUA Table & File manipulation addon

#1 Post by attackspeedzero » Fri Mar 29, 2013 3:29 pm

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.
Last edited by attackspeedzero on Mon Apr 29, 2013 4:40 pm, edited 1 time in total.

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: LUA Table & File manipulation addon

#2 Post by rock5 » Sat Mar 30, 2013 1:43 am

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.

Code: Select all

table_find_word(table, word) - 
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.
  • 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.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
attackspeedzero
Posts: 28
Joined: Sat Mar 02, 2013 6:24 pm

Re: LUA Table & File manipulation addon

#3 Post by attackspeedzero » Mon Apr 01, 2013 1:59 pm

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!

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests