Page 1 of 1

Problem with utils.stringSplit

Posted: Monday 17 May 2021 12:10
by Sagitarius
Hi Team,
had a first look on utils.stringSplit an copied the script below from forum, but it does not
work, I always get
2021-05-17 08:54:00.332 Status: dzVents: Error (2.4.19): ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:12: attempt to call field 'stringSplit' (a nil value)

Can one of you assist?
BTW I tested some more scripts found at this forum, get always the same error message
Many thanks.

Code: Select all

return {
    active = true,
	on = {
        timer =
        {
            'every 1 minutes'
        },
    },
	execute = function(dz)
	    
    local myString = "A-B-C-D"
   myTable = dz.utils.stringSplit(myString,"-")

    for idx, val in pairs(myTable) do
     dz.log(idx..val)
    end

end

Re: Problem with utils.stringSplit

Posted: Monday 17 May 2021 13:03
by waaren
Sagitarius wrote: Monday 17 May 2021 12:10 ad a first look on utils.stringSplit an copied the script below from forum, but it does not work, I always get
2021-05-17 08:54:00.332 Status: dzVents: Error (2.4.19): ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:12: attempt to call field 'stringSplit' (a nil value)
The version you are on is much too old to give proper support on.

What you could do is include the current utils.stringSplit function directly in your script; ike below example.

Code: Select all

return {
    active = true,
    on = {
        timer =
        {
            'every 1 minutes'
        },
    },
    execute = function(dz)

        local function stringSplit(text, sep, convertNumber, convertNil)
            if not(text) then return {} end
            local sep = sep or '%s'
            local include = '+'
            if convertNil then include = '*' end
            local t = {}
            for str in string.gmatch(text, "([^" ..sep.. "]" .. include .. ")" ) do
                if convertNil and str == '' then str = convertNil end
                table.insert(t, ( convertNumber and tonumber(str) ) or str)
            end
            return t
        end

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

        for idx, val in pairs(myTable) do
            dz.log(idx .. ': ' .. val)
        end

    end
}