3

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

4
  • _G["Map"..l[k]](Process[ks[l[k]]][1], R[1]) Commented Mar 12, 2014 at 16:16
  • @Egor, No since I have double quotes around as well, it's not working, Please have a closer look , (Process["ks.PV1"][1],MsgIn,mg) , This is what exactly I need Commented Mar 12, 2014 at 16:18
  • Can you show what you have in S? Commented Mar 12, 2014 at 17:04
  • @hjpotter92: In S, I have "MSH, PID, PV1, EVN" e.t.c, depending on events, sometimes I have all these 4 , rest of the time any 3 or 2 Commented Mar 12, 2014 at 17:07

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks hjpotter92: by the way, this code worked from me, I figured oit as soon as you suggested about S .Here is the code that worked: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
@Simrankaur You can mark the solution as selected/answer and the question will appear as such in the questions list.
1

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

1 Comment

Consider to upvote (and even accept) an answer that has proven useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.