How to get all instances of a substring in a table?

I am creating a skill tree and knowing how to do what is described in the title would be very helpful for implementation. For example if a part of the skill tree was all indexed with “Health” followed by the number of its level I would want to loop through the skill tree and just return every table whos key begins with the substring “Health”. This way I can easily find the highest level the player has upgraded instead of having to do if 1 then if 2 then if 3 . . . Does anybody know a string method which can help accomplish my goal?

image

Here is an image what dictionary would look like.

String.sub() Sounds like it can help you. Here is an Example code to see which one is the max health, as well as filtering the Health Stats only.


local SkillTree = { ["Health1"] = {}, ["Health2"] = {}, ["Health3"] = {} } --Ex Table for My usage
local HighestHealthLevelFound = 0
for CheckString, Value in SkillTree do 
	if string.sub(CheckString, 1, 6) == "Health" then 
		print(CheckString.." Is a Health STAT, and at Level "..tonumber(string.sub(CheckString, 7)).." Health")
		if HighestHealthLevelFound < tonumber(string.sub(CheckString, 7)) then 
			HighestHealthLevelFound  =  tonumber(string.sub(CheckString, 7))
		end
	end
end


print("Highest Health level found in list was "..HighestHealthLevelFound)

Hope this helps!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.