local function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={} ; local i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        t[i] = str
        i = i + 1
    end
    return t
end

local function split2(inputstr, sep)
    sep=sep or '%s'
    local t={}
    for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do 
        table.insert(t,field) 
        if s=="" then
            return t 
        end 
    end 
end

local matchId       = ARGV[1]
local keys          = split2(ARGV[2], ",")
local values        = split2(ARGV[3], "#")
local curTimeStamp  = ARGV[4]
local result        = {}

for index = 1, #keys, 1 do
    if(values[index] ~= nil and values[index] ~= '') then
        local val = split(values[index], ",")
        table.insert( result, values[index] )
        redis.call("HMSET", keys[index], unpack(val))
        redis.call("HSET", keys[index], "LASTUPDATE", curTimeStamp)
    end
end

return result
