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

sorting a string to tables algorithm?

this is hard to explain for me, i need a to create a 2d table according to how a string is inputted, the tables are created for each delimiter it finds in the string, first delimiter i use is "," - which creates a tables inside the current table index for each alphanumeric character it finds in the message, the second delimiter i use is "|" - it creates a new table, and every word before "," it finds after "|" will be put into the new table

heres what i have:

local function SortMessageToTable(Message, Delimiter)
    local out = {}
    for word in (Message .. Delimiter):gmatch("(%w-)" .. Delimiter) do
        table.insert(out, word)
    end
	
    return out
end

local SortedMessage = SortMessageToTable(Message, "|")
for i = 1, #SortedMessage do
	SortedMessage[i] = SortMessageToTable(SortedMessage[i], ",")
end

what i'm trying to get: input = "test1, test2 | test2, test1"

result = table1: {"test2", "test2"} table2: {"test1", "test1"}

what i want to be getting is = table1: {"test1", "test2"} table2: {"test2", "test1"}

for some reason it just gets the last word it finds before "|" and sets every field in the table index to it. does anyone know how to fix this? it would be very appreciated

1 comment

0

anyone?

1 answer

✓ Accepted answer

The output of your script is not what you're saying... so I'm not sure what you're doing.


"%w-" .. Delimiter requires every word ("%w-") to be immediately followed by the delimiter.

Use what I suggested before: "(.-)" .. Delimiter or "[^" .. Delimiter .. "]*" .. Delimiter or something similar.

1 comment

0

thanks again, blue. i just have a hard time wrapping my brain around this kind of stuff

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