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

string patterns, split sets of strings into various tables? [closed]

Asked by
pidgey 548 Moderation Voter
7 years ago

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

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
Edited 7 years ago

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
0
woah, thank you so much. love the way you solved my problem, thank you for making it easy to understand pidgey 548 — 7y
Ad