Jump to content




[Lua][Question]Calling Functions Dynamically With Table Values


  • You cannot reply to this topic
2 replies to this topic

#1 Smiley43210

  • Members
  • 204 posts

Posted 19 April 2013 - 07:38 AM

Hey guys,

Is there any way to call a function using a table for arguments? The number of arguments can change depending on user input. For example, if a table is set up as { "edit", "myFile" }, I want to call shell.run() with the table values as the arguments. Another example: the table is { "myFile", "arg1", "arg2", "arg3" }. I would need to call shell.run("myFile", "arg1", "arg2", "arg3"). Since the number of arguments needed can change, hardcoding
local var1 = myTable[1]
local var2 = myTable[2]
and so on wouldn't be efficient. How can I do this?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 April 2013 - 07:53 AM

unpack(). `shell.run(unpack(myTable))`

Also, the shell no longer requires individual arguments--it will take the whole command string in the first argument just fine. You could therefor use table.concat too, if you wanted.

#3 Smiley43210

  • Members
  • 204 posts

Posted 19 April 2013 - 10:23 AM

Ah, thanks! Also, good to know about shell.run; it's much simpler.

Edit: I also just remembered loadstring(), but the above works just fine.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users