Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

string patterns, split sets of strings into various tables?

ok this topic confuses me

how would i make a string pattern that gets every digit & uppercase or lowercase letter or whitespace and puts the string into a table. if it finds a specific character then it stops, then gets the position of the character that was found and continues off putting strings after that character into a different table

for example character to find is '|'

"1dsi,gok cnmx| do46,d"

table 1 = {"1dsi", "gok cnmx"}
table 2 = {" do46", "d"}

i did this at first

for Word in string.gmatch(Message, "(%w*%s*),*|?") do
	table.insert(SortedMessage, Word)
end

and for that, the output for

"1ad, fds h4 o |as56"

is

{"1ad", " fds h4 o ", "as56"}

i just have no idea how to split the rest of the groups into a different table after '|', and another one if it finds another after, etc

i tried to split it into groups but i failed miserably, heres my attempt on that

SortMessageToTable = function(Message)
		local SortedMessage = {
			
		}
		
		local CurrentTable = {}
		for w,i in string.gmatch(Message, "(%w*),*()") do
			table.insert(CurrentTable, w)
			print(w)
			if Message:sub(i, i) == "|" then
				table.insert(SortedMessage, CurrentTable)
				
				CurrentTable = {}
			end
		end
		table.insert(SortedMessage, CurrentTable)
		
		return SortedMessage
	end

and this is what i get for this mess: http://i.imgur.com/kazWUry.gifv

also, here's the code that reads the returned table

local SortedMessage = Commands.SortMessageToTable(Message)
		
		for i=1, #SortedMessage do
			for v=1, #SortedMessage[i] - 1 do
				print(i, SortedMessage[i][v])
			end
		end

help would be appreciated

1 answer

✓ Accepted answer

If I understand you right, you want to split into groups by |, and then split those into groups by ,.

For example, "ab,cd|e|f,g,h" should be mapped to {{"ab", "cd"}, {"e"}, {"f", "g", "h"}}.

In that case, just first split by "|" and then split each by ",":

-- EXAMPLE:
-- splitString("foo||bar", "|") returns {"foo", "", "bar"}
function splitString(str, by)
	local out = {}
	for word in (str .. by):gmatch("(.-)" .. by) do
		table.insert(out, word)
	end
	return out
end

local input = "1dsi,gok cnmx| do46,d"
local splitBar = splitString(input, "|")
for i = 1, #splitBar do
	splitBar[i] = splitString(splitBar[i], ",")
end

1 comment

0

woah, thank you so much. love the way you solved my problem, thank you for making it easy to understand

This discussion is preserved as it was. New answers, comments, and votes are closed.