added .stringSplit

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

added .stringSplit

Post 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
The most dangerous phrase in any language is:
"We always done this way"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: added .stringSplit

Post 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
Last edited by waaren on Friday 10 May 2019 11:48, edited 1 time in total.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: added .stringSplit

Post by emme »

cool!

really cool! 8)
The most dangerous phrase in any language is:
"We always done this way"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: added .stringSplit

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest