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

Split string(with a designated way to separete)

Started by lukasloko, 23 June 2015 - 04:17 PM
lukasloko #1
Posted 23 June 2015 - 06:17 PM
I want to split strings , but not with the default paterns like this:

for token in gmatch("lalla", [^%p]+) do
or with %a %c %d %l %w etc…

i want to do that but with a designated patern like this:
(not a real code , you know)

for token in gmatch("la:la\la.la", "\")

then the results will be : la:la and la.la

(sorry for any English error)
Creator #2
Posted 23 June 2015 - 06:26 PM
What exactly do you mean?
lukasloko #3
Posted 23 June 2015 - 06:32 PM
anything like split string with my , i choice , the term of the separation.

without use a patern like separete with ponctuation by %p
Creator #4
Posted 23 June 2015 - 06:34 PM
ok, here it goes:


local function split(str,sep)
  sep = sep or " "
  local ret = {}
  for token in str:gmatch("[^"..sep.."]+") do
	 ret[#ret+1] = token
  end
return ret end
Edited on 23 June 2015 - 04:35 PM
lukasloko #5
Posted 23 June 2015 - 06:41 PM
so with this the result will come in a table?

I need to use the table or i can make with other thing?
lukasloko #6
Posted 23 June 2015 - 06:57 PM
ok , it works , thanks
Creator #7
Posted 23 June 2015 - 07:19 PM
ok , it works , thanks

You are welcome!