Reputation: 4941
I have functions named
MapMSH(Msg.MSH, b)
MapPID(Msg.PID, b)
MapPV1(Msg.PV1,b)
NOW, in another function from where I will be calling above three functions, I have a variable u
looping through values MSH
, PID
and PV1
I know I need to use ..
operator to concatenate strings.
what I really want is the value in u
to be concatenated to Map, something like
"Map"..u(Msg.u, b)
so that my functions get called automatically as soon as the value in u
matches.
With the syntax above, it says unexpected symbol near "Map"
Can someone please tell me the exact syntax for that?
Upvotes: 1
Views: 1380
Reputation: 2161
Try this if your functions are in the global namespace.
funcList = {"MSH","PID","PV1"}
for _,u in pairs(funcList) do
_G["Map"..u](Msg[u],"X")
end
Test
Msg = { MSH="MSH", PID="PID", PV1="PV1" }
function MapMSH(a, b)
print( a..b )
end
function MapPID(a, b)
print( a..b )
end
function MapPV1(a, b)
print( a..b )
end
Output
MSHX
PIDX
PV1X
Your problem is very similar to this problem.
Upvotes: 2