Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

sorting a string to tables algorithm? [closed]

Asked by
pidgey 548 Moderation Voter
7 years ago

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

0
anyone? pidgey 548 — 7y

Locked by User#24403

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

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.

0
thanks again, blue. i just have a hard time wrapping my brain around this kind of stuff pidgey 548 — 7y
Ad