This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Left's profile picture

Split.

Started by Left, 05 April 2013 - 06:56 PM
Left #1
Posted 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.
Mads #2
Posted 05 April 2013 - 09:41 PM
How about trying a google search?
Try googling "lua split string", and look at the results.
Kingdaro #3
Posted 06 April 2013 - 12:15 AM
Here'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 %.
1lann #4
Posted 06 April 2013 - 12:44 AM
Here'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 %.
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 do

return unpack(parts)
Smiley43210 #5
Posted 06 April 2013 - 07:09 PM
Just a general side note, don't go asking for code that other people have written.
Left #6
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!
Cloudy #7
Posted 07 April 2013 - 12:46 AM
I agree, google is your friend.
theoriginalbit #8
Posted 07 April 2013 - 06:48 AM
posting a split function with COMMENTS!
here is Kingdaro's function, that he so kindly posted, commented…


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
SuicidalSTDz #9
Posted 07 April 2013 - 07:24 AM
I agree, google is your friend.
As is Dogpile, Ask, Yahoo (To an extent), Bing (But It's Not Google), and many more :P/>

EDIT:


  local parts = {} -- 'this is a new table'
Ya don't say.. :)/>
theoriginalbit #10
Posted 07 April 2013 - 08:09 PM
As is Dogpile, Ask, Yahoo (To an extent), Bing (But It's Not Google), and many more :P/>
And stackoverflow, don't forget stackoverflow… and the PIL, the PIL is handy too.

Ya don't say.. :)/>
Hey he wanted it commented, so I commented it :P/>
SuicidalSTDz #11
Posted 08 April 2013 - 03:52 AM
And stackoverflow, don't forget stackoverflow… and the PIL, the PIL is handy too.
Yes, all hail the PIL and Stackoverflow. They are our saviors for all things Lua!

Hey he wanted it commented, so I commented it :P/>
>:U
theoriginalbit #12
Posted 08 April 2013 - 03:54 AM
They are our saviors for all things Lua!
Well stackoverflow is the saviour for all languages for me!
SuicidalSTDz #13
Posted 08 April 2013 - 04:39 AM
They are our saviors for all things Lua!
Well stackoverflow is the saviour for all languages for me!
>:U
Eat my non-emoticonic face!