• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Find value in string

Xagul

deathzot.net
Joined
Jun 30, 2008
Messages
1,295
Solutions
3
Reaction score
1,043
Thank you for taking a look at my problem!

I would like to make a function that returns a value located somewhere in a string. This string will not always be the same so it must locate the pattern and then pull the value from there.

For example:

Code:
string = level (100)
           magic (50)
           shield (10)

Now the script would search that string and find the value for magic which would return 50. I figured it would be easier having ( ) around the values so you have a reference point for the search however they can be removed if it does not work as well with them there.
 
LUA:
string = [[
level (100)
magic (50)
shield (10)
]]

function getVal(str, val)
	local a,b = str:find(val .. '%s%(%d+%)')
	return a and b and str:sub(a, b):gsub('%D', '')
end

print(getVal(string, 'level'))
Output:
Code:
100
 
Last edited:
Ok so I am trying to learn from this and I am having some trouble understanding how it works. I would like to know what

Code:
%s%(%d+%)

means in the line

Code:
local a,b = str:find(val .. '%s%(%d+%)')

I am guessing they are like variables or something? If it is easier you can also direct me to a website which lists these variables.
 
  1. %s matches a space character
  2. %( is the equivalent of (, I had to put % before it since it's a magic character
  3. %d+ matches a number of any length
  4. %) is same as 2
 
Awesome that helps a ton, thanks :)


"You must spread some Reputation around before giving it to Cykotitan again." =\ it never lets me rep you! >:[
 
Back
Top