• Grab all of k before /n
    14 replies, posted
I have a tool tip set to k, and I need it to grab all information that goes before the /n like this. "[I][B]Cost: $17[/B][/I]\nHealth: 20" Then set it to a variable. Any help would be appreciated.
local split = string.Explode("\n", k) myvar = split[1]
[lua]local var = k:match("(.-)\n")[/lua]
Now is there any way to flip that data (a string) into an integer?
Wait, that's not what you want. Give me a second. [lua]local var = "Cost: $17\nHealth: 20" for str in var:gmatch("[^n]+") do local int = tonumber(string.Trim(string.sub(str , string.find(str , ":") + 1 , string.len(str)):gsub("$" , ""))) print(int) end [/lua] Untested but shows the idea.
tonumber?
I think I overdid it. [lua]local str = "Cost: $17\nHealth: 20" local kv = {} local function DoStuff( str ) for b in str:gmatch( "([^\n]+)" ) do local k,v = b:match( "(%S+):%s*(%S+)" ) kv[k] = v end end DoStuff( str ) print( kv["Cost"], kv["Health"] )[/lua]
None of these seem to work. Is there any other way, or can you explain these because my brain hurts.
Oh well. Fuck efficiency, let's go for clear, working code. [lua]local str = "Cost: $17\nHealth: 20" for k , v in ipairs(string.Explode("\n" , str)) do -- For each different newline character, do this local bstr = string.sub(v , string.find(v , ":") + 1 , string.len(v)) -- Start at the position of : + 1 , end at the end of the string local int = tonumber(string.Trim(bstr:gsub("$" , ""))) -- Replace $ with a blank character, then remove whitespaces at the end and start of the string. end [/lua]
bad argument #2 to 'tonumber' (base out of range) said console. what could i be doing wrong?
Try this. [lua]local str = "Cost: $17\nHealth: 20" for k , v in ipairs(string.Explode("\n" , str)) do -- For each different newline character, do this local bstr = string.sub(v , string.find(v , ":") + 1 , string.len(v)) -- Start at the position of : + 1 , end at the end of the string local int = tonumber(bstr:gsub("$" , "")) -- Replace $ with a blank character, then remove whitespaces at the end and start of the string. end[/lua]
Nothing changed.
[lua] local str = "Cost: $17\nHealth: 20" for k,v in pairs(string.Explode("\n",str)) do local _,_,num = v:find("^.+(%f)$") --%f is probably wrong, it should be th char for a number num = tonumber(num) end [/lua] Untested
It doesnt return any errors, but later on when I use: if pl:GetNWInt("Cash") < num then It returns saying that it cant compare a number to nil, so apparently that just returns with nothing. and by the way, what are the _,_, for?
[QUOTE=almost cool;21131690]It doesnt return any errors, but later on when I use: if pl:GetNWInt("Cash") < num then It returns saying that it cant compare a number to nil, so apparently that just returns with nothing. and by the way, what are the _,_, for?[/QUOTE] If you read the quote, I said that %f is most likely wrong. _,_ is the start and end positions returns from string.find, which we don't want:v:
Sorry, you need to Log In to post a reply to this thread.