Text Parsing Tutorial: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>WildSurvival
Adding CatUp
>NXTBoy
Creating the script: This was absolutely terrible, and is still pretty bad. Patterns, anyone?
Line 16: Line 16:
First off, lets establish our [[variables]].
First off, lets establish our [[variables]].


<pre>
local lastKey = 0 -- This is needed for the ParseText [[function]]
LastKey = 0 -- This is needed for the ParseText [[function]]
local allStrings = {} -- Were all strings will be put into
AllStrings = {} -- Were all strings will be put into
</pre>


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


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


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


<pre>
function ReplaceString(str, find, replace)  
function ReplaceString(String,Replace,ReplaceKey)  
    str = tostring(str)
String = tostring(String)
    strStart, strEnd = str:find(find) -- Finding if there really is a key to be replaced and finding the character number.
A,B = string.find(String,Replace) -- 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 ""
NewString = string.sub(String,1,A-1)..ReplaceKey..string.sub(tostring(String),B+1,String:len()) -- Replacing the "|" with ""
end
return NewString
end
</pre>


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


<pre>
lastKey = 0 -- This is needed for the ParseText function
LastKey = 0 -- This is needed for the ParseText function
allStrings = {} -- Were all strings will be put into
AllStrings = {} -- Were all strings will be put into


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


function ParseText(String,EndKey)
function ParseText(text, endKey)
StrLength = #String
    for i = 1, #text do
PLength = #EndKey
        if text:sub(i,i) == endKey then -- This right here is detecting the letter/symbol/number on that line
  for i=1, StrLength do
            if lastKey == 0 then -- Checking to see see if a "|" was found already. This will be needed for the next function
      if string.sub(String,i,i) == EndKey then -- This right here is detecting the letter/symbol/number on that line
                table.insert(allStrings, ReplaceString(text:sub(1,i), EndKey, "")) -- Inserts the string into a table :D
        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
            table.insert(AllStrings,ReplaceString(string.sub(String,1,i),EndKey,"")) -- Inserts the string into a table :D
            else
            LastKey = i + 1 -- Setting the LastKey to the current key
                table.insert(allStrings, ReplaceString(text:sub(lastKey, i), EndKey, ""))-- Inserts the string into the table
        else
                --[[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]]
            table.insert(AllStrings,ReplaceString(string.sub(String,LastKey,i),EndKey,""))-- Inserts the string into the table
            end
            --[[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 |]]
         end
         end
      end
    end
  end
end
end


</pre>
</pre>
Line 83: Line 70:
Time to print each result
Time to print each result


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


==See also==
==See also==

Revision as of 15:42, 3 August 2011

Introduction

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, lets establish our variables.

local lastKey = 0 -- This is needed for the ParseText function
local allStrings = {} -- Were 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 = {} -- Were 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
            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