Mathematical Operations & Comparison on matches table

Share your scripts and packages with other Mudlet users.
Post Reply
Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Mathematical Operations & Comparison on matches table

Post by Denarii »

This script will allow you to perform mathematical operations and comparison on the matches table without having to add tonumber() into all your triggers and aliases.

+, -, /, *, %, ^ and unary - will fail if you use them with a value that can't be converted to a number.
Comparisons will work as expected if both values can be converted to a number, or both are actual strings, and will return false if one operand converts to a number and the other does not.

Code: Select all

setmetatable( matches, {
	["__add"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			return (o1 + o2)
		end
	end,
	["__sub"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			return (o1 - o2)
		end
	end,
	["__div"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			return (o1 / o2)
		end
	end,
	["__mul"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			return (o1 * o2)
		end
	end,
	["__mod"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			return (o1 % o2)
		end
	end,
	["__pow"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			return (o1 ^ o2)
		end
	end,
	["__unm"] = function(op) 
		local o = tonumber(op)
	    if o then
			return -o
		end
	end,
	["__eq"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			if (o1 == o2) then
				return true
			else
				return false
			end
		elseif ((o1 and (not o2)) or (o2 and (not o1))) then
				return false
		elseif ((not o1) and (not o2)) then
			if (op1 == op2) then
				return true
			end
		end
	end,
	["__lt"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			if (o1 < o2) then
				return true
			else
				return false
			end
		elseif ((o1 and (not o2)) or (o2 and (not o1))) then
				return false
		elseif ((not o1) and (not o2)) then
			if (op1 < op2) then
				return true
			end
		end
	end,
	["__le"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			if (o1 <= o2) then
				return true
			else
				return false
			end
		elseif ((o1 and (not o2)) or (o2 and (not o1))) then
				return false
		elseif ((not o1) and (not o2)) then
			if (op1 <= op2) then
				return true
			end
		end
	end,
	["__gt"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			if (o1 > o2) then
				return true
			else
				return false
			end
		elseif ((o1 and (not o2)) or (o2 and (not o1))) then
				return false
		elseif ((not o1) and (not o2)) then
			if (op1 > op2) then
				return true
			end
		end
	end,
	["__ge"] = function(op1, op2) 
		local o1, o2 = tonumber(op1), tonumber(op2)
		if (o1 and o2) then
			if (o1 >= o2) then
				return true
			else
				return false
			end
		elseif ((o1 and (not o2)) or (o2 and (not o1))) then
				return false
		elseif ((not o1) and (not o2)) then
			if (op1 >= op2) then
				return true
			end
		end
	end,
})

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: Mathematical Operations & Comparison on matches table

Post by Caled »

Heiko, might I suggest that this one be considered to be included with Mudlet as a default?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Mathematical Operations & Comparison on matches table

Post by Vadi »

I think this was already proposed in irc, but I don't remember the outcome. At any rate I support this. It's like the matches table not being nil for capture groups you have, but actually smarter this time :roll:

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: Mathematical Operations & Comparison on matches table

Post by Caled »

This one will not compile, for me.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Mathematical Operations & Comparison on matches table

Post by Vadi »

It compiled alright for me.

Post Reply