Can anyone explain how we will get the output of the following code in Lua? - lua

function ReturnTwoVal()
return "1","2"
end
function ReturnThreeVals()
return "x","y","Z"
end
TblA = {ReturnThreeVals(),ReturnTwoVal() }
print(TblA[2],TblA[1], TblA[2], TblA[3], TblA[4])
Output will be: 1 x 1 2 nil

Expressions that return multiple values are adjusted to a single value, unless they are the last expression in a function call or table constructor.
Therefore,
TblA = {ReturnThreeVals(),ReturnTwoVal() }
is equivalent to
TblA = {"x", "1","2"}

Related

calling a function with a table constructor?

i was learning on how to create and use a class on a youtube video and i came across this code
cc = {calo = 0, goal = 1500}
function cc:new(t)
t = t or {}
setmetatable(t,self)
self.__index = self
return t
end
a = cc:new{goal = 100}
i dont understand this part a = cc:new{goal = 100} this is the first time where i see a function being called with anything other then (). i have 2 guesses on what this does maybe it replaces the parameter of cc:new function with {goal = 100} or maybe the function is called and t table is assigned to the variable then assigning the table with {goal = 100}? pls correct me if im wrong
first, {goal = 100} is just an argument.
second, cc:new{goal = 100} is equal to cc:new({goal = 100})
this is a syntactic sugar, you can call a function without brackets if theres only one argument and its type is string or table literal
example:
function foo(x)
print(x)
return foo
end
foo "Hello" "World"
this will output "Hello" and "World"
if you want to call a function without brackets and using multiple arguments, the function you gonna call must return another function for the next argument.
the another function is not always the original one
it may be recursive

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)

Ignore Returned Value

local meshId = message:sub(message, message:find(message, "/hat%s%d"), message:find(message, "/hat %d+"))
The message:find() returns two values; the first character and the last character. How would I make it only return the last character?
Simply, I think solution. just second prameter return
function returnTwo(...)
local a, b = message:find(...)
return b
end
wrap message:find function and return second value
How about?
If a function returns more than one parameter, you can use select(2, functioncall()) to get the second parameter. For example:
function returntwo() return "first", "second" end
print(select(2, returntwo())) -- prints "second"
In the case in question, you'd use it as local meshId = message:sub(message, select(2, message:find(message, "/hat%s%d")), select(2, message:find(message, "/hat %d+")))

Passing value as a parameter in Lua

A = {}
function A:text()
return 100
end
print(A["text"]()) -- prints "100"
----------------------------------
A = {}
function A:text(value)
return value
end
print(A["text"](100)) -- prints "nil"
Is there a way that I can pass a value as a parameter and return the same value?I need to loop through 5 functions...
You could, if you declared your function correctly.
function A:text(value)
This creates a function that takes two parameters. The : is what's responsible for that. The first parameter is an implicitly declared parameter called self. The second is value. This function is intended to be called as A:text(100) or with A["text"](A, 100).
These are for class-member-like functions.
You should instead create the function like this:
function A.text(value)
This creates a function that takes one parameter.
As "Nicol Bolas" pointed out, I add table/self parameter and it worked fine.
-- from "A["text"](100)" to "A["text"](self, 100)" or "A["text"](A, 100)"
A = {}
B = {"text", "type"}
function A:text(value)
return "text "..value
end
function A:type(value)
return "type "..value
end
for i=1, 3 do
for j=1, #B do
print(A[B[j]](self, i)) -- prints "text 1 type 1 text 2 type 2 text 3 type 3"
end
end

Lua - Can a function be called from a table value and its value returned

I'm asking for help regarding this matter as I cannot quite get the grasp of if it is possible let alone if I am just doing it wrong.
my = {
Question = AskQuestion()
}
function AskQuestion()
return "What do you want to know?"
end
My intention is to do, print(my.Question) and have it return "What do you want to know?", So far I have just ran into headaches and errors trying to get this to work.
Further on should this be solved I can presume rather than figuring it out myself I may as well ask in addition. What about passing on a value to the function.
my = {
Answer = MyAnswer(vAnswer)
}
function MyAnswer(vAnswer)
if vAnswer == "42" then return "Life.." end
end
So here I would want to do, print(my.Answer(42)) and it return "Life.."
Just invert your declaration:
function AskQuestion()
return "What do you want to know?"
end
my = {
Question = AskQuestion()
}
Since Lua is interpreted you have to have everything defined before you can use.
When setting a variable you don't need to pass the (vAnswer) because it is not a defition, it is just a existing-function usage, so change to:
function MyAnswer(vAnswer)
if vAnswer == "42" then return "Life.." end
end
my = {
Answer = MyAnswer
}
I am assuming here that you are using a different my table, but you should get the picture.
A function is a first class entity in Lua so you can set a variable or table field to be a function:
function f() end -- do nothing
a = f
Then a refers to f, you can do to/with a as you can with f, such as calling it:
a() -- calls f()
Then you can set a to be another function, and call it:
function g(x) end
a = g -- a is now function g
a(1) -- calls g(1)
Everything in the above examples can be done with table fields too:
tt = {
f = function() print('hi') end,
g = function(x) print(x) end
}
tt.f() -- calls tt.f function
a = tt.f
a() -- calls tt.f
tt.h = a
tt.h() -- calls tt.f
tt.f = print -- now tt.f is print function
tt.f('bye') -- prints 'bye'

Resources