Thread Tools Display Modes
09-09-12, 11:46 PM   #1
Brusalk
An Aku'mai Servant
 
Brusalk's Avatar
AddOn Author - Click to view addons
Join Date: May 2010
Posts: 32
A few questions about Lua pattern matching

So in one of my addons I need access to information on how often a DoT/HoT ticks on a unit.

One of the ways I'm experimenting with is pulling the hasted tick time from the tooltip itself on UNIT_AURA event.

My code:

Code:
addEvent(function(self, event, unit)
	for i=1, 40 do
		for _, spellbar in ipairs(ns.spellbars.active) do
			if spellbar.spellConfig.debuff[1] > 0 then -- We have a debuff to look at
				if unit == (type(spellbar.spellConfig.debuff[2]) == "string" and spellbar.spellConfig.debuff[2] or "target") then
					local name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID, canApplyAura, isBossDebuff, value1, value2, value3 = UnitDebuff(unit, i)
					if caster == "player" then
						if spellbar.spellConfig.debuff[1] == spellID then -- Ooh, we found it on this spellbar.
							ns.tooltip:SetUnitDebuff(unit, i)
							local scanText = _G["EventHorizonScanTooltipTextLeft2"]:GetText()
							local tickSpeed = tonumber(string.sub(scanText, string.find(scanText, "every ")+6, string.find(scanText, " sec")-1))
								--tonumber returns nil if it can't be converted to a number
							ns:addDebuff(spellbar, duration, tickSpeed)
							
						end
					end	
				end
			end
		end
	end
end,
"UNIT_AURA")
A cursory inspection of wowhead seems to show that all DoTs/HoTs tooltips say "every x.xx sec".
I'm not versed in Lua pattern matching at all and I managed to get the x.xx information using what I have below, but on a non-matching string it returns garbage information. tonumber() luckily returns nil if the number can't be converted, but I'm sure that there's more efficient/robust ways of doing it using pattern matching.

Does anyone know of a pattern which would match only the x.xx portion of the string to make it more efficient? (Or any other changes to my above code to make it more efficient)

Thanks
-Brusalk
Code:
tonumber(string.sub(scanText, string.find(scanText, "every ")+6, string.find(scanText, " sec")-1))
  Reply With Quote
09-10-12, 02:15 AM   #2
Rochet2
A Defias Bandit
Join Date: Sep 2012
Posts: 3
I dont know if its more efficent, but this should work:
Code:
local str = "623 Shadow damage every 3.55 seconds every 3.5522seconds." -- Example
local time = tonumber(str:match("every ([%d.]+) sec"))
print(time, type(time)) -- time is nil in case no numbers or decimals or the every .. sec is not found. (even without the tonumber, but it will be in string form)
  Reply With Quote
09-10-12, 02:50 AM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Code:
tonumber(scanText:match("every (%d+%.%d+) sec"))
tonumber() seems to be handing a nil return fine as of WoW 5.0.4. Also note this would only be able to match on an enUS client, labels would be different in other locales. For more information on patterns and also functions that use them, here's a link to the official Lua documentation on it.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » A few questions about Lua pattern matching


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off