Page 1 of 2
Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 5:59 pm
by captn_retardo
Hello all,
I am attempting to load a string from a file, and then coercing that string into the function name. For example, I would like to do something like:
Code: Select all
function test_func()
print("yes!")
end
str = "test_func"
str()
So that end part, where I str() is obviously wrong, as a string is not a function. I am at a loss at how to do this. Does anyone have any tricks they can share?
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 6:08 pm
by Robin
Try using loadstring:
Code: Select all
function test_func()
print("yes!")
end
str = "test_func"
assert(loadstring(str))()
Disclaimer: I didn't test it.
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 6:10 pm
by bartbes
Try this:
Code: Select all
function test_func()
print("yes!")
end
str = loadstring("test_func()") --notice the (), better syntax..
str()
--or..
str = "test_func()"
loadstring(str)() --however, this converts to a function every time
Robin beat me, but I hope this is going to help anyway
EDIT: though robin's suggestion of using assert is good..
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 6:22 pm
by Robin
We both were
almost right. I think this is the way to go:
Code: Select all
function test_func()
print("yes!")
end
str = assert(loadstring("test_func()"))
str()
Explanation:
"test_func()" is the string that gets executed.
loadstring creates a function (or was it called "chunk" or something like that?), containing the code from a string
assert gives an error if something went wrong, otherwise passes the function to str
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 6:29 pm
by bartbes
So.. what I said (if you include my edit)..
I kind of feel like saying "I was better", but I won't.. or will I?
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 6:49 pm
by Robin
bartbes wrote:I kind of feel like saying "I was better", but I won't.. or will I?
I say we call it a draw
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 7:11 pm
by captn_retardo
Thank you so much for the quick response. Loadstring() is exactly what I was looking for.
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 8:00 pm
by Jake
Rather than loading the string as a piece of Lua you can just call the function from the global table
I would prefer to do that because it seems safer to me.
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 8:09 pm
by bartbes
And what if you want to do more than just calling a single functions without arguments?
Re: Requesting assistance coercing a string to a function call
Posted: Thu Jun 11, 2009 8:11 pm
by Jake
Depends on exactly what but surely it's quicker just to do a table lookup, right?