lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Cheers David. Just what I needed--saves me a lot of code.
Sean.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of David Given
Sent: 12 January 2010 10:56
To: lua@bazar2.conectiva.com.br
Subject: Re: calling a function who's name is stored in a lua table

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 12/01/10 10:50, sean farrow wrote:
[...]
> Now if I know the value of the number of the function in the table I wish
> to call (x) how can I call this without iterating through the
> table-essentially is there a way of finding a value in a table given it's
> key?

All globals, including functions, are (usually) stored in a special
table called _G. So all you need to do is:

function callfunction(n, ...)
  local name = AtcActions(n)
  local f = _G[name]
  return f(...)
end

e.g.:
print(callfunction("2", "word"))

- -- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "Sufficiently advanced incompetence is indistinguishable from
│ malice." -- Vernon Schryver
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAktMVVYACgkQf9E0noFvlzgyuQCfeToqGEGqnglyd4N56ewjEHGs
hmgAoIRGb6U27xb/9l8/DiHR2GXXzs7M
=XLyy
-----END PGP SIGNATURE-----