get index of subtable from function args in lua - function

Im trying to return the value of an index of a table which is inside of another table and am doing so using the args given when a function is run so that the arg resolves to a variable name.
function getsubindex(varname,index)
local tbl = {}
tbl.first = 99
tbl.subTbl = {10,20,30}
if not index then
return tbl[varname]
else
return tbl[varname[index]]
end
end
returning tbl[varname] works because you can use a string as an index for example.
getsubindex("first")
Would return 99, but I need to get into the subTbl take this example of running the func.
getsubindex("subTbl",2)
I'm wanting this to return 20

Change:
return tbl[varname[index]]
to:
return tbl[varname][index]

Related

Return function

i want more understand about return function. Is return function the function have a return value,what is that mean ? Plis give example and explanation about function have a return value and not have a return value.
return is a statement for functions, so let's say you have a bit of code like print(myFunction()).
myFunction's return value is what print is printing.
so if I have
# define function
def myFunction():
# this gives "Hello World" to whatever runs it
return "Hello World"
# put the return value in a variable
a = myFunction()
# print the variable data
print( a )
when you run a = myFunction the function is returning "Hello World" and storing it in the variable a. then you're just printing value of a.
Think of it this way,
Sometimes you want your function to do something for example add something to a variable:
a = 1
def add_1(x):
x += 1
# This would add 1 to the variable a. No return needed.
add_1(a)
# Now a is 2.
but sometimes you would want a function to return something that you could work with. For example printing it or saving it to another variable:
a = 1
def add_1(x):
return x + 1
b = add_1(a)
# The function returns a + 1, which is 2, and saves it to the variable b.
print (add_1(b))
# The function prints the returned value of b + 1, which is 3 and would print 3.
Hopefully this was helpful.

Function with for loop - How to return in vb | UFT

I would need support for that. I guess it's an easy one but i would like to get different approach if possible.
I have the following Function and i just want to return value. If possible within a string variable.
StrVal = "123 Test"
Function IsHaving(StrVal)
Set reg1 = New RegExp
reg1.Gloabl=True
reg1.IgnoreCase=False
reg1.Pattern="\d+"
Set mats = reg.Execute(StrVal)
For Each mat In mats
return mat.Value
Next
End Function
And then passing the value returned in another string
StrNum = IsHaving(StrVal)
I would like to do this way but i am not sure that in vb i can return from a loop for ( within the Function).
Some ideas on that ?

What Meaning Of This Function

function [TC]=Translate(T0,Base)
end
I know that Translate is a function and T0 and Base his parameter but what is [TC]?
Octave (and matlab) have a rather unique way of returning variables from functions. Instead of defining explicitly what to return from the function using a return keyword, they define from the outset which variables will be returned when the function exits, and octave simply looks for those variables by name at the time the function exits, and returns their values, whatever they may be by that point.
Your function may return nothing:
function returnsNothing();
disp('hello, I return nothing');
end
or it may return one output:
function Out = returnsOne(x)
Out = x+5
disp('This function will return the value of Out');
end
or it may return more than one outputs:
function [Out1, Out2] = returnsTwo(x)
Out1 = x+5;
Out2 = x+10;
end
You would call the last function from the octave terminal (or script) like this:
[a,b] = returnsTwo(5); % this will make a = 10 and b = 15

How to get the variable a function is assigned to?

I need to get the variable a function's return value is assigned to.
function example()
end
variable = 3 --just for example
variable = example() --I need the value of variable(in this case 3) passed as an argument to example()
Please read the Lua reference on how to call and define functions.
https://www.lua.org/manual/5.3/manual.html#3.4.10
https://www.lua.org/manual/5.3/manual.html#3.4.11
You can simply do this:
function fancyFunction(val)
return val
end
local val = 3
val = fancyFunction(val)
of course this minimum example does not make much sense. You should of course have another return value than your argument. Otherwise you don't have to call the function in the first place.
(Quite hard to understand what you're trying to do.) Could you possibly be referring to object oriented programming? If so, then you need something like this:
--setup
t = {}
function t:set(v) self.value = v end --assign value to 'variable'
function t:get() return self.value end
--use
t:set(3)
print(t:get())

Lua - Execute a Function Stored in a Table

I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks!
Here is how the table was defined:
game_level_hints = game_level_hints or {}
game_level_hints.levels = {}
game_level_hints.levels["level0"] = function()
return
{
[on_scene("scene0")] =
{
talk("hint0"),
talk("hint1"),
talk("hint2")
},
[on_scene("scene1")] =
{
talk("hint0"),
talk("hint1"),
talk("hint2")
}
}
end
Aaand the function definitions:
function on_scene(sceneId)
-- some code
return sceneId
end
function talk(areaId)
-- some code
return areaId
end
EDIT:
I modified the functions so they'll have a little more context. Basically, they return strings now. And what I was hoping to happen is that at then end of invoking the functions, I'll have a table (ideally the levels table) containing all these strings.
Short answer: to call a function (reference) stored in an array, you just add (parameters), as you'd normally do:
local function func(a,b,c) return a,b,c end
local a = {myfunc = func}
print(a.myfunc(3,4,5)) -- prints 3,4,5
In fact, you can simplify this to
local a = {myfunc = function(a,b,c) return a,b,c end}
print(a.myfunc(3,4,5)) -- prints 3,4,5
Long answer: You don't describe what your expected results are, but what you wrote is likely not to do what you expect it to do. Take this fragment:
game_level_hints.levels["level0"] = function()
return
{
[on_scene("scene0")] =
{
talk("hint0"),
}
}
end
[This paragraph no longer applies after the question has been updated] You reference on_scene and talk functions, but you don't "store" those functions in the table (since you explicitly referenced them in your question, I presume the question is about these functions). You actually call these functions and store the values they return (they both return nil), so when this fragment is executed, you get "table index is nil" error as you are trying to store nil using nil as the index.
If you want to call the function you stored in game_level_hints.levels["level0"], you just do game_level_hints.levels["level0"]()
Using what you guys answered and commented, I was able to come up with the following code as a solution:
asd = game_level_hints.levels["level0"]()
Now, asd contains the area strings I need. Although ideally, I intended to be able to access the data like:
asd[1][1]
accessing it like:
asd["scene0"][1]
to retrieve the area data would suffice. I'll just have to work around the keys.
Thanks, guys.
It's not really clear what you're trying to do. Inside your anonymous function, you're returning a table that uses on_scene's return value as keys. But your on_scene doesn't return anything. Same thing for talk.
I'm going to assume that you wanted on_scene and talk to get called when invoking each levels in your game_level_hints table.
If so, this is how you can do it:
local maxlevel = 99
for i = 0, maxlevel do
game_level_hints.levels["level" .. i] = function()
on_scene("scene" .. i)
talk("hint" .. i)
end
end
-- ...
for levelname, levelfunc in pairs(game_level_hints.levels) do
levelfunc()
end