LUA tuple and function "pointers"

Started by
5 comments, last by quasty 19 years, 10 months ago
Hi, I've read some about the LUA tables. I didn't have much success in realizing it but the thing I want would be a table with references to functions - I've seen that it's somehow possible (for example the table.foreach function) but how do I manage it: I want a set of values assigned to a function or simply callable code:

t = { 
{"valA1", "valA2", func_valA1},
{"valB1", "valB2", func_valB}, 
}
 
Now I want to search for certain value compinations and execute the given function to that entry. I don't really know how to do it.. ??? Here I've used tables in tables - is there another way to store "tuples" in lua - example like the multi return values of functions? thankx alot! [edited by - quasty on June 11, 2004 2:56:02 AM]
Advertisement
Well, you're touching on a few things here.

First of all, your example is a bit unclear. Do you want to, say, execute func_valA1 when some variable is set to "valA1" and some other variable is set to "valA2"? Or do you want to execute func_valA1 when some variable is set to either valA1 or valA2? Either is possible, but the techniques will differ.

With that said, referring to functions in tables is easy. You can either make the functions and then refer to them by name, or you can declare them inline. Watch:

function calculate(op, a, b) do    local optable = {         add = function(a,b) do return a+b end,        sub = function(a,b) do return a-b end,        mul = function(a,b) do return a*b end    }    return optable[op](a,b)end-- Example of usecalculate("sub", 7, 3) -- prints 4

Even naming "optable" is optional, but I left it in there for the sake of readability.

"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on June 11, 2004 3:09:40 AM]
Thank you, but I''m afraid I didn''t understand it right.

What I meant a pre-defined table with elements. Every element is composed of a set of (string) values in a certain way. The last element should be a "pointer" to a function.

At the moment I start my lua skript these table is already full of element. What I want is to search the list (I only have a set of strings) and look up the function assigned to the string elements.
In that case, the only really nontrivial part is the lookup.

Your current representation is a little unwieldy. You''d need to do a linear search through it, which is much slower than it needs to be.

I suggest you have the table keys be the string attributes concatenated into single strings, and the values be the functions. That way, you can just concatenate the desired string values, look that key up in the table, and execute the resultant function.

"Sneftel is correct, if rather vulgar." --Flarelocke
Hi,

I''m afraid I''ve again a bit of a problem...
I''ve been trying it over the weekend and got it finally working - for strings. The one thing i still don''t understand:

t = {["abc"] = foo }function foo()   print("foo")end 
How do I execute the function foo? Simply >t["abc"] isn''t working and I couldn''t find anything about it in the ref-manual or Wiki.

thanks alot!
t["abc"] is a function. So to call it, use t["abc"]()

Or for better readability, use t.abc()
thanks alot to both of you - now everything works fine :)

This topic is closed to new replies.

Advertisement