3

Is it possible in LUA to execute a function like foo("param1, param2, param3, param4"), and have it detect it as foo(param2, param2, param3, param4)

Why? I have a scenario where a function can receive as many parameters as I wish, and they can't be sent as a list (It's in CoronaSDK, so I can't really modify the source, and have to adapt to it). So, sometimes I'll have to send 3 parameters, while sometimes I'll be sending 100. Is there anyway of doing this?

4 Answers 4

4

they can't be sent as a list (It's in CoronaSDK, so I can't really modify the source, and have to adapt to it)

Sure it can. Watch:

function CallWithParametersInAList(FunctionToCall, paramsInList)
  return FunctionToCall(unpack(paramsInList))
end

See? Every array element in paramsInList will be unpacked into arguments to FunctionToCall. Also, every return value from FunctionToCall will be returned.

see

function test(a)
    print("this is lua test function."..a);
end

CallWithParametersInAList(test,{33333});

OUTPUT

this is lua test function.33333
Sign up to request clarification or add additional context in comments.

Comments

3

You can call a lua function with as many parameters as you want. If the function expects more parameters, they will be set to nil. If the function expects too few parameters, the extra parameters sent will get ignored.

For example:

function f(a, b, c)
  print(a, b, c)
end

f(1, 2) -- prints: 1 2 nil

f(1, 2, 3) -- prints: 1 2 3

f(1, 2, 3, 4, 5) -- prints: 1 2 3

edit:

If you must get the parameters from a string and those parameters include things like tables and functions, you have little option but to get the string parsed by loadstring function:

-- s contains the parameters
s = '1,2,{1,2,3}'

-- f is the function you want to call
loadstring('f(' .. s .. ')')()  -- prints: 1  2  table: 0061D2E8

I'm not sure about CoronaSDK, but the loadstring function tends to be a bit slow. Try to avoid it if possible.

2 Comments

The OP want to convert a string into an argument list.
@lhf: It's an XY problem; he's trying to solve the actual problem (call a function with arbitrary parameters) the wrong way.
0

One of the best methods, in my opinion(and the one I use) is something like this:

function Call( ... )
    -- All passed arguments are stored in a default table named arg
    table.foreach( arg, print )
end

And, here's a working example on codepad - oDmVZ209.

1 Comment

This is Lua 5.0 syntax, deprecated in Lua 5.1 and removed in Lua 5.2 (both concerning arg table table.foreach function).
0

I'd second Nicol Bolas that you are probably trying to solve the wrong problem, but if you still want to parse the string and turn it into a list of parameters, here is one way to do this (loadstring is not available in Corona environment; obviously this doesn't handle any type of hierarchical data):

function str2list(s)
  local parms = {}
  for p in s:gmatch("([^,]+),?") do table.insert(parms, p) end
  return unpack(parms)
end
print(str2list("param1, param2, param3, param4"))

Comments

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.