Text Parsing Tutorial

From Legacy Roblox Wiki
Jump to navigationJump to search

Parsing text is where you seperate the elements and nodes from the values. Say I had this single line of string

String = "5,100,4,5|10,100,3,2|"

Parsing it would seperate each value from each. But for now, were sticking with seperating from the |'s and not the ,'s

We want it to seperate into this: 5,100,4,5 10,100,3,2

Creating the script

First off, let's establish our variables.

local lastKey = 0 -- This is needed for the ParseText function
local allStrings = {} -- Where all strings will be put into

Ok, we have our variables. Let's get to work on the function.

function ParseText(text, endKey)
    for i = 1, #text do
        if text:sub(i,i) == endKey then -- This right here is detecting the letter/symbol/number on that line
            if lastKey == 0 then -- Checking to see see if a "|" was found already. This will be needed for the next function
                lastKey = i + 1 -- Setting the lastKey to the current key
            end
        end
    end
end

Okay, now we need the ReplaceString fuction. This will be to remove the "|"'s from the string.

function ReplaceString(str, find, replace) 
    str = tostring(str)
    strStart, strEnd = str:find(find) -- Finding if there really is a key to be replaced and finding the character number.
    return str:sub(1, strStart-1)..replace..str:sub(strEnd+1, #str) -- Replacing the "|" with ""
end

Now we need to add it all together and add to the ParseText function

 lastKey = 0 -- This is needed for the ParseText function
 allStrings = {} -- Where all strings will be put into

 function ReplaceString(str, find, replace) 
     str = tostring(str)
     strStart, strEnd = str:find(find) -- Finding if there really is a key to be replaced and finding the character number.
     return str:sub(1, strStart-1)..replace..str:sub(strEnd+1, #str) -- Replacing the "|" with ""
 end

 function ParseText(text, endKey)
     for i = 1, #text do
         if text:sub(i,i) == endKey then -- This right here is detecting the letter/symbol/number on that line
             if lastKey == 0 then -- Checking to see see if a "|" was found already. This will be needed for the next function
                 table.insert(allStrings, ReplaceString(text:sub(1,i), EndKey, "")) -- Inserts the string into a table :D
                 lastKey = i + 1 -- Setting the lastKey to the current key
             else
                 table.insert(allStrings, ReplaceString(text:sub(lastKey, i), EndKey, ""))-- Inserts the string into the table
                 --[[This is were the lastKey was needed. If we wouldnt of had the last key and used 1 instead, it would of returned the whole string because of string.sub. The last key in this string in the first case that the "else" has been called would be 5 + 1 which is |This is were the lastKey was needed. If we wouldnt of had the last key and used 1 instead]]
             end
         end
     end
 end

Now all they are seperated into all values for each, player or whatever your storing per |. You could use the same ParseText function on the ","'s.

Time to print each result


ParseText("5,100,4,5|10,100,3,2|","|")
for i,v in pairs(allStrings) do
    print(v)
end

See also