Text Parsing Tutorial: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>WildSurvival Edited Internal Links |
>Anaminus fixed bad syntax |
||
(13 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{CatUp | Tutorials}} | |||
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 13: | Line 13: | ||
==Creating the script== | ==Creating the script== | ||
First off, | 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. | Ok, we have our variables. Let's get to work on the function. | ||
function ParseText(text, endKey) | |||
function ParseText( | 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 | |||
end | |||
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. | ||
function ReplaceString(str, find, replace) | |||
function ReplaceString( | 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 | |||
end | |||
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> | <pre> | ||
lastKey = 0 -- This is needed for the ParseText function | |||
allStrings = {} -- Where all strings will be put into | |||
function ReplaceString( | 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 | |||
end | |||
function ParseText( | 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 | |||
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. | ||
Time to print each result | Time to print each result | ||
ParseText("5,100,4,5|10,100,3,2|","|") | ParseText("5,100,4,5|10,100,3,2|","|") | ||
for i,v in pairs( | for i,v in pairs(allStrings) do | ||
print(v) | print(v) | ||
end | end | ||
==See also== | ==See also== | ||
[[ | *[[Function Dump/String Manipulation|String manipulation]] | ||
*[[Function Dump/Table Manipulation|Table manipulation]] | |||
[[ | *[[Tables]] | ||
*[[String Patterns]] | |||
[[ | [[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