Question

I am working on Lua and I have this kind of code

MapMessage(Process["ks.MSH"][1], MsgIn, mg)
MapEvent(Process["ks.EVN"][1], MsgIn, mg)
MapPatient(Process["ks.PID"][1], MsgIn, mg)
MapVisit(Process["ks.PV1"][1],MsgIn,mg)

In above statements, MapMessage, MapEvent, MapPatient, MapVisit are the functions and ks.MSH, ks.EVN, ks.PID, ks.PV1 are the tables in the database. Now, I want to automate a part of this process using gmatch function provided in lua and I have this so far

for u in string.gmatch(S, "([^,%s]+)"), 1 do
     l[k] = u 
    _G["Map"..l[k]](Process["ks[l[k]]"][1], R[1])
     k=k+1
   end 

but the concatenation part in the third line of above code is not really making it ks.MSH, ks.PID, ks.PV1 e.t.c, so please suggest what needs to be there in place of (Process["ks[l[k]]"][1]to get s.MSH, ks.PID, ks.PV1 e.t.c

Was it helpful?

Solution

Since your string contains "MSH, PID, PV1, EVN", you'd have to use a hash-table or a lookup table. The program would be something like this:

S = "MSH, PID, PV1, EVN"
tLookup = {
    MSH = "Message",
    EVN = "Event",
    PID = "Patient",
    PV1 = "Visit",
}

for u in S:gmatch "([^,%s]+)" do
    sNameOfFunction = tLoopup[u]
    _G[ "Map"..sNameOfFunction ] ( Process["ks."..u][1], MsgIn, mg )
     k=k+1
end

Or even something like this:

S = "MSH, PID, PV1, EVN"
tLookup = {
    MSH = _G.MapMessage,
    EVN = _G.MapEvent,
    PID = _G.MapPatient,
    PV1 = _G.MapVisit,
}

for u in S:gmatch "([^,%s]+)" do
    tLoopup[u] ( Process["ks."..u][1], MsgIn, mg )
    k = k+1
end

OTHER TIPS

Here is what finally worked, thanks Egor and hjpotter92 :)

ks = {MSH = "ks.MSH", EVN = "ks.EVN", PID = "ks.PID", PV1 = "ks.PV1", PV2 = "ks.PV2"}
S = tostring(R[1].AllSegmentsList)
 l = {}
k = 1
for u in string.gmatch(S, "([^,%s]+)") do
 l[k] = u 
 _G["Map"..l[k]](Process[ks[l[k]]][1], MsgIn, mg)
   k=k+1
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top