PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Parsing Text Values

Parsing Text Values

Jul 03 2018, 10:54 AM PST 5 min

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

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

One way to parse it would be to break this into smaller units so a script can process individual values. Let’s say we want it to separate into these components:

  • 5,100,4,5
  • 10,100,3,2

Creating the script

First off, let’s establish our Articles/Variables|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 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 function. 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 where the lastKey was needed. If we didn't have the last key and used 1 instead, it would've 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]]
            end
        end
    end
end

Now all they are separated into all values for each, player or whatever you’re 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
Tags:
  • lua
  • coding
  • concept