Go Back   WowAce Forums > General > Lua Code Discussion
Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding].

Reply
 
Thread Tools
Old 01-05-2011   #1
alezandros
Member
 
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
Default making variables from strings

hey all,

sorry to bug again but i cant seem to get the addon i'm working on to register that my variables should be numbers. not strings. but for some reason when i load the addon it loads with an error saying it cant do the arithmetic for string value. when i looked over all the code i diddnt find any links to strings so i might just not be typing it up right i dunno :/

Code:
function LvlWizRev_EstXpTime()
local lvltime = LevelTime
local UXP=UnitXP("PLAYER")
ET = UXP / lvltime * 3600
print (ET)
--if ET==0 then ET="inf."end
--LvlWizRev_Tooltip:AddLine("|cFF6495EDEstimated time to level up: "..ET.."|r")

end
this is the function i'm having issues with and it tells me that lvltime is the problem "string"

Code:
    elseif (event == "TIME_PLAYED_MSG") then
          -- Remember play time
          AllTime = SecondsToTime(a1);
       LevelTime = SecondsToTime(a2);
is what's in my Onevent function that grabs the initial data

and these are my session variables(note some of them actually dont do anything i need to clean these up lol):

Code:
local LevelTime =0
local lvltime = 0
local LWR_currXP = 0
local LWR_exptogo = 0
local LWR_Init = nil
local WML = 0
local NUMMOBS = 0
local TPL = 0
local TX = 0; --(total amount of xp needed to level)--
local UXP = 0; --(current amount of xp aquired)--
local PT = 0 --(current amount of played time)--
local lxp = {} --(current player XP)--
local mkc=0  --(kills needed on specific mob to level up)--
local lastkillXP=0 --(last kill xp reward)--
local TL= {} --(level of the current target selected)--
local LWR_prevXP=0
PLT = 0
local prev_update = 0
local prev_update_delay = 4
local LvlWizRev_Update_Freq = 1

then finally it all is supposed to wrap in nice and neatly into my LvlWizRev_ShowTooltip() function which works for everything else. i've been over this bit of code trying to tweak and change it but i usually end up with a string error or cant do an upvalue on nil, so i'm lost lol any help is greatly appreciated!
alezandros is offline   Reply With Quote
Old 01-05-2011   #2
ailae
Junior Member
 
Join Date: Sep 2008
Posts: 68
Default Re: making variables from strings

Code:
LevelTime = SecondsToTime(a2);
SecondsToTime returns a string. SecondsToTime(3600) returns "1 Hr" (in enUS). So you should set lvltime to a2.
__________________
I'm not crying. It's just been raining on my face.
ailae is offline   Reply With Quote
Old 01-05-2011   #3
LaoTseu
Senior Member
 
Join Date: Jun 2006
Posts: 491
Default Re: making variables from strings

SecondsToTime() return a formatted string, not a number. I don't know where you get a2 in your TIME_PLAYED_MSG event processing function but, assuming it's a number of seconds, you need to keep that value.

My advice would be to use SecondsToTime() only when you want to display the string and only keep the "raw" numerical value until you need to display it.

If you really need to keep the localized and formatted value, put it in separate variable, for example LevelTimeText.
LaoTseu is offline   Reply With Quote
Old 01-05-2011   #4
alezandros
Member
 
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
Default Re: making variables from strings

thanks for the help guys, i switched the LevelTime call to just = a2 then made a secondary call in the function to convert to SecondsToTime() to get a time format. i also found a way to add more lines into it for a more precise look

i guess i shouldof figured SecondsToTime() was a string lol :P
alezandros is offline   Reply With Quote
Old 01-07-2011   #5
alezandros
Member
 
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
Default Re: making variables from strings

just out of curiosity, is it possible to pull a number out of a string and use the number value in math? i tried like in the following but i never get an exact number, just a lower number

Code:
X = string.find("some number of 45", "%d+")
but when i call that it doesnt return the number 45 it returns something less which makes it no good for use in math if i need the number in question to be pulled
alezandros is offline   Reply With Quote
Old 01-07-2011   #6
Xinhuan
Asian Sheep Lover
 
Xinhuan's Avatar
 
Join Date: Aug 2007
Location: Singapore
Posts: 4,033
Default Re: making variables from strings

The first 2 numbers that string.find() returns is the starting character and ending character number.

Code:
local x, y = string.find("some number of 45", "%d+")
x, y will be 16 and 17 respectively because "4" is the 16th character in the string and "5" is the 17th character.

What you want is

Code:
local _, _, x = string.find("some number of 45", "(%d+)")
The () around the %d+ tells the find() function to also return the actual number as the 3rd return. This is called a string capture. Or you can


Code:
local x = string.match("some number of 45", "(%d+)")
match() doesn't return the first 2 results, just the remainder of the string captures.
__________________
Author/Maintainer of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, Routes, HandyNotes and some others.
Xinhuan is offline   Reply With Quote
Old 01-07-2011   #7
egingell
Amazing Member
 
egingell's Avatar
 
Join Date: May 2006
Location: Cenarion Circle
Posts: 1,582
Send a message via AIM to egingell Send a message via MSN to egingell
Default Re: making variables from strings

FYI: string.match() (and string.find() for that matter) always returns a string. So if you need to do arithmetic on the returned value, you need to pass it to tonumber() (e.g. x = tonumber(x)).
__________________
WoWWiki has moved! Please, make a note of it.
My Addons (a handy dandy list).
My Characters (yes, I have a guild with only me in it).
egingell is offline   Reply With Quote
Old 01-07-2011   #8
alezandros
Member
 
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
Default Re: making variables from strings

ahh i see... another thing that is puzzling me that i was looking up in the lua refrence manual and i cant seem to find... what is _, ? is that like ome unused variable?
alezandros is offline   Reply With Quote
Old 01-07-2011   #9
Adirelle
Legendary Member
 
Adirelle's Avatar
 
Join Date: Dec 2006
Posts: 2,392
Default Re: making variables from strings

"_" is a legit variable name, as would be "x" or "a". In Lua, it is often used as a placeholder to discard return values, e.g.:

Code:
local _, _, x = string.find("some number of 45", "(%d+)")
This assigns the first two return values from string.find to _. _ then contains the value of the second return value (since it is assigned last) but we will never use it.

One could also write it like this:

Code:
local x = select(3, string.find("some number of 45", "(%d+)"))
But this implies a function call, which may be slower than assigning to local variables. Moreover, using _ is sometimes more flexible, like this:

Code:
local start, _, x = string.find("some number of 45", "(%d+)")
Here we only discard the second return value.
__________________
Author of AdiButtonAuras, AdiBags, Squire2 and several other addons.

Each time you hit your "copy" command with a block of code, think about a way to refactor it so it did what you want without using the "paste" command.
Adirelle is offline   Reply With Quote
Old 01-07-2011   #10
OrionShock
Legendary Member
 
OrionShock's Avatar
 
Join Date: May 2006
Location: Arizona
Posts: 3,787
Default Re: making variables from strings

the underscore _ is used as the common "i don't care about that return" variable. Just remember to use a local _ not the global _ ( IE [[ local _, a = someFunc() ]] vs [[ _, a = sumfunc() ]] )
__________________
Author of GuildCraft, SickOfClickingDailies, CursorCooldown, Broken_LFD
WoWAce Addon List WoWInterface Addon List

"I was there in the beginning... and things were very different back then" --An Echo from a time before.
OrionShock is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

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

Forum Jump


All times are GMT. The time now is 05:57 PM.