Text Parsing Tutorial: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>Emess No edit summary |
>WildSurvival Adding CatUp |
||
Line 1: | Line 1: | ||
{{CatUp | Tutorials}} | |||
==Introduction== | ==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 93: | Line 94: | ||
*[[Function_Dump/Table_Manipulation|Table manipulation]] | *[[Function_Dump/Table_Manipulation|Table manipulation]] | ||
*[[Tables]] | *[[Tables]] | ||
[[Category: Scripting Tutorials]] |
Revision as of 21:22, 9 July 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.
LastKey = 0 -- This is needed for the ParseText [[function]] AllStrings = {} -- Were all strings will be put into
Ok, we have our variables. Let's get to work on the function.
function ParseText(String,EndKey) StrLength = #String PLength = #EndKey for i=1, StrLength do if string.sub(String,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(String,Replace,ReplaceKey) String = tostring(String) A,B = string.find(String,Replace) -- 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 NewString 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(String,Replace,ReplaceKey) String = tostring(String) A,B = string.find(String,Replace) -- 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 NewString end function ParseText(String,EndKey) StrLength = #String PLength = #EndKey for i=1, StrLength do if string.sub(String,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(string.sub(String,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(string.sub(String,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 |]] 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