Step 1 - Writing the Lua function

This page describes a sample Lua function that will take an input string, split it into 10 strings based on a comma (",") character, and then return the array of 10 strings to the trigger action.

Note that the input parameters from the trigger action are passed using the DWInput object, and the output parameters are returned through the DWOutput object.

Sample script

The sample Lua script file can be downloaded and used: stringsplit.lua

function split(str, delim, maxNb)
    -- Eliminate bad cases
    if string.find(str, delim) == nil then
        return { str }
    end
    
    if maxNb == nil or maxNb < 1 then
        maxNb = 0  -- No limit
    end
    
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end
function stringsplit()
    DWOutput["OUT"] = split(DWInput["IN"],",",10)
    return 0
end


Go to: Step 2 - Uploading the Lua script file to the Staging browser