Store function to indexed arrays and call him with undefault param - lua

I'm trying to create and indexed array of functions to call him with changed params, like this:
local function wubba(lubba)
return lubba
end
local dub = {
["wubba"] = {func = wubba(lubba)}
}
print(dub["wubba"].func("hi"))
But in all my tries i got errors, i can't figure out how to do it. Anyone can help me?
lua: wubba.lua:9: attempt to call field 'func' (a nil value)
stack traceback:
wubba.lua:9: in main chunk
[C]: in ?

Solved, just not to store with params:
local dub = {
["wubba"] = {func = wubba}
}

Related

How to pass a table key in a function for use in a for loop?

For some reason it doesn't appear to work to pass in a table key as a function argument, what is the trick to do this?
I'm trying to wrap the for loop iteration technique in vanilla Lua into a function that has three arguments: (1) the table to iterate, (2) the table_key to check each time, and (3) the value to find. If a match is found, return it, otherwise return nil.
function table_find_match(table, table_key, match_value)
for i=1, #table do
local this = table[i]
if this[table_key] == match_value then
return this[table_key]
end
end
return nil
end
local table_example = {
{
key_example = "string_value_1"
},
{
key_example = "string_value_2"
}
}
local result = table_find_match(table_example, key_example, "string_value_1")
print(result)
Found a solution, if I pass in the table key as a string it works, such as
table_find_match(table_example, "key_example", "string_value_1")
but I really dislike having to convert it into a string, if anyone knows any other workaround to this please share
If you pass it like table_find_match(table_example, key_example, "string_value_1")
the key_example is now considered as a (nil) variable if not defined before executing, so it has to be like
local key_example = "key_example"
local result = table_find_match(table_example, key_example, "string_value_1")
print(result)

call function into function in lua

i have code in my lua file and i edit that to look like this
function getUserinfo(user_id)
function call_back_user_info(status , result)
t = {["first_name"]= result.first_name_, ['have_access']= result.have_access_, ["last_name"]=result.last_name_,["user_name"]=result.username_}
return t
end
getUser(user_id,call_back_user_info)
end
i need to return t table value when i call getUserinfo function.but it is get me a nil value !
note :getUser function puts data in to call_back_user_info
how i can resolve this problem? thank
You can't do a "long return" which returns from an outer function from inside of an inner function.
But what you can do is create a local variable which is closed over, like this:
function getUserinfo(user_id)
local t
function call_back_user_info(status , result)
t = {["first_name"]= result.first_name_,
['have_access']= result.have_access_,
["last_name"]=result.last_name_,
["user_name"]=result.username_}
end
getUser(user_id,call_back_user_info)
return t
end

Getting access to parameter inside a string being executed with 'load'

I would like to know if there's a way for the 'load' function to get a variable value from a local variable instead of a global variable ?
Say that I've got a string like this 'trade.version == 2' that I want to execute with the 'load' function inside a function taking the trade as parameter.
function doTest( trade, test )
-- inside the string 'test', I would like that any reference to 'trade'
-- refer to the function parameter instead of a global variable
if ( assert(load("return "..test))() ) then
-- do something
end
end
a_userdata = { version = 2 }
-- Calling the function above
doTest( a_userdata , "trade.version == 2" )
[string "return trade.version == 2"]:1: attempt to index global 'trade' (a nil value)
stack traceback:
[string "return trade.version == 2"]:1: in main chunk
stdin:2: in function 'doTest'
stdin:1: in main chunk
[C]: in ?
As a workaround, I have defined a global variable and it's working pretty fine.
But I would like to avoid this global variable.
Thank you very much
function doTest( trade, test )
-- inside the string 'test', I would like that any reference to 'trade'
-- refer to the function parameter instead of a global variable
if assert(load("local trade = ...; return "..test))(trade) then
-- do something
print('Yes, version equals to 2')
end
end
a_userdata = { version = 2 }
-- Calling the function above
doTest( a_userdata , "trade.version == 2" )

lua's loadstring() not working with tables

I have some text and I'm trying to load it via load string. The following works:
local m = loadstring("data = 5")()
But when the data is a table it doesn't work and gives the error "attempt to call a nil"
local m = loadstring("data = { 1 = 10}")()
The table declaration in lua require integer keys to be put inside square brackets:
data = {
[1] = value,
}
The enclosing of keys in square brackets is always allowed, valid and possible. It can be skipped iff your key follows the pattern: [A-Za-z_][A-Za-z0-9_]* (which is the same as a valid variable name in lua)
If you had added an assert you would have got a more helpful message:
local m = assert (loadstring("data = { 1 = 10}"))()
Result:
stdin:1: [string "data = { 1 = 10}"]:1: '}' expected near '='
stack traceback:
[C]: in function 'assert'
stdin:1: in main chunk
[C]: ?
And to actually answer the question, unless the table key happens to follow Lua variable naming rules, you have to put it inside square brackets, eg.
local m = assert (loadstring("data = { [1] = 10}"))()
m is still nil when I do this
What does that matter? The loadstring is done.
Just do this:
assert (loadstring("data = { 1 = 10}"))()
print (data [1])
You don't need the variable m. The loadstring puts a table into data - that is the important thing.

How to delete class variable in Torch?

I have problems in understanding how a class variable in Torch works.
I did the following:
mydata=torch.class('something')
I checked the User variable by typing who() and it shows:
== User Variables ==
[_RESULT] = table - size: 0
[mydata] = table - size: 0
[something] = table - size: 0
I first of all tried to delete mydata by
mydata=nil
it works. mydata is now freed and can be reinitialized to any values. But when I tried to delete the variable something by typing
soemthing=nil
It seems it's not working even though the variable something was not list in who() anymore. When I try:
mydata2=torch.class('something')
the error pops out:
/data/torch/install/share/lua/5.1/torch/init.lua:65: something has been already assigned a factory
stack traceback:
[C]: in function 'newmetatable'
/data/torch/install/share/lua/5.1/torch/init.lua:65: in function 'class'
[string "mydata2=torch.class('something')"]:1: in main chunk
[C]: in function 'xpcall'
/data/torch/install/share/lua/5.1/trepl/init.lua:648: in function 'repl'
/data/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
[C]: at 0x00406670
Could anyone tell me the reason behind it?
torch.class() stores the class metatable in the lua-registry, see http://www.lua.org/pil/27.3.1.html and the luaT_lua_newmetatable() function in the torch C-backend.
In order to unregister an existing class it is necessary to remove the entry from the lua-registry. You can access the registry from lua with help of the debug.getregistry() function.
With removeal from registry your example works:
mydata = torch.class('something')
mydata = nil
soemthing = nil
-- remove the global registration:
debug.getregistry()['something'] = nil
-- now it is possible to register the class again
mydata2 = torch.class('something')

Resources