Text Parsing Tutorial: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>NXTBoy
Creating the script: This was absolutely terrible, and is still pretty bad. Patterns, anyone?
>Anaminus
fixed bad syntax
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{CatUp | Tutorials}}
{{CatUp | Tutorials}}
==Introduction==
Parsing text is where you seperate the elements and nodes from the values. Say I had this single line of string
Parsing text is where you seperate the elements and nodes from the values. Say I had this single line of string


Line 14: Line 13:


==Creating the script==
==Creating the script==
First off, lets establish our [[variables]].
First off, let's establish our [[variables]].


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


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.
Line 40: Line 39:


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 = {} -- Where all strings will be put into


  function ReplaceString(str, find, replace)  
  function ReplaceString(str, find, replace)  
Line 63: Line 62:
     end
     end
  end
  end
</pre>
</pre>
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.
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.


Line 77: Line 74:


==See also==
==See also==
*[[Function_Dump/String_Manipulation|String manipulation]]
*[[Function Dump/String Manipulation|String manipulation]]
*[[Function_Dump/Table_Manipulation|Table manipulation]]
*[[Function Dump/Table Manipulation|Table manipulation]]
*[[Tables]]
*[[Tables]]
*[[String Patterns]]
[[Category: Scripting Tutorials]]
[[Category: Scripting Tutorials]]

Latest revision as of 06:40, 5 May 2012

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