I need a split function for my Interface/General library. Anyways can you please post the SPLIT FUNCTION YOU HAVE CODED and have comments for it. By the way only post it if you can give me permission for it.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Split.
Started by Left, 05 April 2013 - 06:56 PMPosted 05 April 2013 - 08:56 PM
Hia!
I need a split function for my Interface/General library. Anyways can you please post the SPLIT FUNCTION YOU HAVE CODED and have comments for it. By the way only post it if you can give me permission for it.
I need a split function for my Interface/General library. Anyways can you please post the SPLIT FUNCTION YOU HAVE CODED and have comments for it. By the way only post it if you can give me permission for it.
Posted 05 April 2013 - 09:41 PM
How about trying a google search?
Try googling "lua split string", and look at the results.
Try googling "lua split string", and look at the results.
Posted 06 April 2013 - 12:15 AM
Here's one that'll work in most cases.
Of course, the "divider" in this case needs to be escaped, as dividers with magic characters such as * or + will screw it up if not escaped with %.
function split(str, divider)
local parts = {}
for v in str:gmatch('[^'..divider..']+') do
table.insert(parts, v)
end
return parts
end
Of course, the "divider" in this case needs to be escaped, as dividers with magic characters such as * or + will screw it up if not escaped with %.
Posted 06 April 2013 - 12:44 AM
Tip when using Kingdaro's if you prefer it unpacked (Like when functions return multiple arguments) Just in case if you didn't know how, just simply doHere's one that'll work in most cases.function split(str, divider) local parts = {} for v in str:gmatch('[^'..divider..']+') do table.insert(parts, v) end return parts end
Of course, the "divider" in this case needs to be escaped, as dividers with magic characters such as * or + will screw it up if not escaped with %.
return unpack(parts)
Posted 06 April 2013 - 07:09 PM
Just a general side note, don't go asking for code that other people have written.
Posted 06 April 2013 - 08:16 PM
Just a general side note, don't go asking for code that other people have written.
OFF TOPIC. This was giving people a chance to get a credit and also to help me by posting a split function with COMMENTS! Please DONT!
Posted 07 April 2013 - 12:46 AM
I agree, google is your friend.
Posted 07 April 2013 - 06:48 AM
here is Kingdaro's function, that he so kindly posted, commented…posting a split function with COMMENTS!
function split(str, divider) -- 'this is a comment saying that str is the string, and divider is the divider to split the string with'
local parts = {} -- 'this is a new table'
for v in str:gmatch('[^'..divider..']+') do -- 'this is a comment that tells you to look up Lua patterns on Google, because it will explain it better and easier than can be explained here without just copy and pasting from a Google result'
table.insert(parts, v) -- 'put the part into a table'
end -- 'end the for loop'
return parts -- 'return the table we just made of the parts'
end
Posted 07 April 2013 - 07:24 AM
As is Dogpile, Ask, Yahoo (To an extent), Bing (But It's Not Google), and many more :P/>I agree, google is your friend.
EDIT:
Ya don't say.. :)/>local parts = {} -- 'this is a new table'
Posted 07 April 2013 - 08:09 PM
And stackoverflow, don't forget stackoverflow… and the PIL, the PIL is handy too.As is Dogpile, Ask, Yahoo (To an extent), Bing (But It's Not Google), and many more :P/>
Hey he wanted it commented, so I commented it :P/>Ya don't say.. :)/>
Posted 08 April 2013 - 03:52 AM
Yes, all hail the PIL and Stackoverflow. They are our saviors for all things Lua!And stackoverflow, don't forget stackoverflow… and the PIL, the PIL is handy too.
>:UHey he wanted it commented, so I commented it :P/>
Posted 08 April 2013 - 03:54 AM
Well stackoverflow is the saviour for all languages for me!They are our saviors for all things Lua!
Posted 08 April 2013 - 04:39 AM
>:UWell stackoverflow is the saviour for all languages for me!They are our saviors for all things Lua!
Eat my non-emoticonic face!