Page 1 of 1

added .stringSplit

Posted: Friday 10 May 2019 11:09
by emme
Ciao,

I've seen in the new stable that dzVenz added a new function: .stringSplit
the docs explain:
stringSplit(string, [separator ]):2.4.19 Function. Helper function to split a line in separate words. Default separator is space.
I'm unclear about the result... will it return a table?

Code: Select all

local myString = "A-B-C-D"
myTable = domoticz.utils.stringSplit(myString,"-")

for idx, val in pairs(myTable) do
   domoticz.log(idx..val)
end
will print
1A
2B
3C
4D
?

I'm unable to test it right now, that's why I'm asking this here :P :P

Re: added .stringSplit

Posted: Friday 10 May 2019 11:33
by waaren
emme wrote: Friday 10 May 2019 11:09 I've seen in the new stable that dzVents added a new function: .stringSplit
I'm unclear about the result... will it return a table?
Yes. I will clarify in the wiki
Effectively it does

Code: Select all

function stringSplit(text, sep)
	local sep = sep or '%s'
	local t = {}
	for str in string.gmatch(text, "([^"..sep.."]+)") do
		table.insert(t, str)
	end
	return t
end
100% pure Lua

Re: added .stringSplit

Posted: Friday 10 May 2019 11:45
by emme
cool!

really cool! 8)

Re: added .stringSplit

Posted: Friday 10 May 2019 11:59
by waaren
emme wrote: Friday 10 May 2019 11:45
I just found out that I missed something when implementing this. Will fix soon but for now, make sure you add

Code: Select all

local utils = require("Utils") 
in scripts where you want to use it.
Then you could also use

Code: Select all

utils.dumpTable(utils.stringSplit("A-B-C-D","-"))
to display the result.