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
8 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 '|'

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

i did this at first

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

and for that, the output for

1"1ad, fds h4 o |as56"

is

1{"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

01SortMessageToTable = function(Message)
02        local SortedMessage = {
03 
04        }
05 
06        local CurrentTable = {}
07        for w,i in string.gmatch(Message, "(%w*),*()") do
08            table.insert(CurrentTable, w)
09            print(w)
10            if Message:sub(i, i) == "|" then
11                table.insert(SortedMessage, CurrentTable)
12 
13                CurrentTable = {}
14            end
15        end
16        table.insert(SortedMessage, CurrentTable)
17 
18        return SortedMessage
19    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

1local SortedMessage = Commands.SortMessageToTable(Message)
2 
3        for i=1, #SortedMessage do
4            for v=1, #SortedMessage[i] - 1 do
5                print(i, SortedMessage[i][v])
6            end
7        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
8 years ago
Edited 8 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 ",":

01-- EXAMPLE:
02-- splitString("foo||bar", "|") returns {"foo", "", "bar"}
03function splitString(str, by)
04    local out = {}
05    for word in (str .. by):gmatch("(.-)" .. by) do
06        table.insert(out, word)
07    end
08    return out
09end
10 
11local input = "1dsi,gok cnmx| do46,d"
12local splitBar = splitString(input, "|")
13for i = 1, #splitBar do
14    splitBar[i] = splitString(splitBar[i], ",")
15end
0
woah, thank you so much. love the way you solved my problem, thank you for making it easy to understand pidgey 548 — 8y
Ad