Looks like you need to really get to know the string.gmatch function(:
string.gmatch Info
The string.gmatch function actually returns an iterator function.. and each time said function is called then it will return any string in the first argument(the string you're 'gmatching') that matches the pattern in the second argument(the pattern you want to 'gmatch').
So, because this returns an iterator function.. can you think of any loops that take in iterator functions? The generic for loop! The syntax for the generic for loop is;
for [values] in [iterator function] do
Since the string.gmatch function only returns 1 value(the matching string) then you only need 1 variable in the '[values]' spot. This variable will hold whatever potential string match, from within the given string, that would match the given pattern.
So I keep talking about 'given string' and 'given pattern' right? What the heck am I talking about?
The string.gmatch function takes in 2 arguments!
String Patterns
When I say "The pattern to match the string" then what i'm meaning is that there are literally syntax patterns to match strings with. There are various patterns.. %d gets numbers.. %a gets letters.. %u gets uppercase letters.. etc..
So String Patterns are literally used for defining patterns that you want to search for in a string. Just an example before we continue - if I wanted to get the letter 'G', then any lowercase letters after that, then i'd use the pattern 'G%l+'. And an example of full usage;
local str = 'Goulstem is so Cool, Goul is God'
local pattern = 'G%l+'
for i in string.gmatch(str,pattern) do
print(i)
end
As you can see, the code printed the three texts.. because those three string of characters matched the given pattern!
Main Answer
So, now that i've sort of walked you through basic understanding of the string.gmatch function and how string patterns operate.. then I think you're ready for the main lesson.
You need to use :%w+ as a pattern.. to find any alphanumeric in the string(uppercase, lowercase, or diget) after a colon. This way, you can capture everything in between the colons. But there's some more to it.. with this pattern then it also captures the colon! So you have to use the string.sub function to seclude the colon from the string capture(:
Once you get the string returned from this pattern then you need to check if it's a number, or a bool.. if it's either or then you need to insert it into a separate table as said datatype.. otherwise keep it a string.
Code
local m = {}
m.Decode = function(str)
local tab = {}
local pattern = ':%w+'
for i in string.gmatch(str,pattern) do
i = i:sub(2)
if i == 'true' then
table.insert(tab,true)
elseif i == 'false' then
table.insert(tab,false)
elseif tonumber(i) ~= nil then
table.insert(tab,tonumber(i))
else
table.insert(tab,i)
end
end
return tab
end
2 comments
Format question: How you you make lines and large text?
Lines: hyphens across the whole pad. Large Text: Text, next line 5 hyphens.