KingofGamesYami, on 07 January 2015 - 06:39 PM, said:
MKlegoman357, on 07 January 2015 - 06:37 PM, said:
No need for reverse here:
local str = ("test.string.three"):match("(.+)%..-")
That's assuming he always has two decimals. I don't think he does.
Actually it does exactly the same as your code and both of our codes have one problem: they will return nil if there is not a single dot in that string. Here's a 'fixed' and simplified version of our both codes:
local function removeLastDecimal ( str )
return str:match( "(.*)%..-" ) or ""
end
Doyle3694, on 07 January 2015 - 06:47 PM, said:
Didn't quite think of this, but how do I convert that string into an adress, making "all.Doors" into all.Doors? Is loadstring() the appropriate function?
If you are using a table then you can index it like this:
local levels = {
one = {
two = 1
}
}
print( levels["one"]["two"] ) --> 1
If you want to get the index from a string like 'one.two' then you would have to parse that string. Something like this should work:
local function getIndex (tab, str)
local t = tab --# a variable to store the last element we could get from the table
for key in str:gmatch("[^%.]+") do --# iterate through every word in a string that doesn't contain a dot in it
if not t[key] then break end --# if the key is nil or false break from the loop
t = t[key] --# get the next value of the table
end
return t --# return the last value we got from the table
end
local levels = {
one = {
two = 1
}
}
print( getIndex(levels, "one.two") )
If you have questions about any part of the code feel free to ask

, it's important that you understand what the code does rather than just blindly copy-paste it.
Edited by MKlegoman357, 07 January 2015 - 07:07 PM.