Timer names, valid values?

Posted by Worstje on Tue 21 Nov 2006 10:20 PM — 3 posts, 10,463 views.

Netherlands #0
Hello,

My problem is as follows. I need to be able to pass a room name, which can contain numbers, spaces and pretty much all basic sentence characters to my timer. The timer name is the only way I can figure out to do this. However, numbers, spaces and I can imagine some other characters are not allowed there (30008 errorcode).

I figured I can hash each roomname into a-z somehow, but I'd need to loop through each key/value combination in my original table afterwards to find out which roomname it corresponds to. This table can get pretty large (100+ entries), so looping whenever a timer fires would be most likely slow things down enough to be noticed with the other 30 plugins I have installed as well. Then again, maybe an algorithm would be slower than looping.

Anyway, there might be a solution I haven't thought about yet. Thanks for all the help, I really appreciate it.
Australia Forum Administrator #1
You can hash the name, see:

http://www.gammon.com.au/scripts/doc.php?function=Hash

I would put a letter at the start, (eg. H) to make sure it starts with a letter and is thus a valid timer name.

Then I would store the hash/name relation into a Lua table, eg.


t = {}

t [hash] = name


Using this you can quickly look up a particular hash to find the original name.
Netherlands #2
I feel like such an idiot. Must be because I posted at 3am or somewhere around that. I asked friends for something resembling a hashing function that wouldn't output numbers, and never thought to look in the mush helpfiles for Hash* after Encode didn't work.

For now, I used another (ugly but working solution):


-- TimerId is a global variable I increase each time I make a new timer.

AddTimer(Letterize(TimerId) .. MakeValidTimerName(roomname), 0, 0, 8, "", 1+4+16384, "ProcessTimer")

-- is used with the following (ugly and hacked) functions:

function MakeValidTimerName(name)
	return string.gsub (name, "[^a-zA-Z]", "")
end

function PickLetter(digit)
	-- a:97 - z:122 ---- A:65 - Z:90
	if (digit < 26) then
		return string.char(digit + 65)
	else 
		return string.char(digit + 97)
	end
end

function Letterize(number)
	local a = (number % 26)
	local b = (number / 26)
	return PickLetter(a) .. PickLetter(b)
end


Whenever I stop feeling lazy, I may switch to the Hash() method. Not sure if it is faster since my function is quite a bit more simple and thus hopefully a bit faster too.