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+")))
Related
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
I have created a function that (pseudo)randomly creates a table containing numbers. I then loop this function until at least correct result is found. As soon as I've confirmed that at least one such result exists, I stop the function and return the table.
When I create tables containing small values, there are no issues. However, once the random numbers grow to the range of hundreds, the function begins to return nil, even though the table is true the line before I return it.
local sort = table.sort
local random = math.random
local aMin, aMax = 8, 12
local bMin, bMax = 200, 2000
local function compare( a, b )
return a < b
end
local function getNumbers()
local valid = false
local numbers = {}
-- Generate a random length table, containing random number values.
for i = 1, random( aMin, aMax ) do
numbers[i] = random( bMin, bMax )
end
sort( numbers, compare )
-- See if a specific sequence of numbers exist in the table.
for i = 2, #numbers do
if numbers[i-1]+1 == numbers[i] or numbers[i-1] == numbers[i] then
-- Sequence found, so stop.
valid = true
break
end
end
for i = 1, #numbers-1 do
for j = i+1, #numbers do
if numbers[j] % numbers[i] == 0 and numbers[i] ~= 1 then
valid = true
break
end
end
end
if valid then
print( "Within function:", numbers )
return numbers
else
getNumbers()
end
end
local numbers = getNumbers()
print( "Outside function:", numbers )
This function, to my understanding, is supposed to loop infinitely until I find a valid sequence. The only way that the function can even end, according to my code, is if valid is true.
Sometimes, more often than not, with large numbers the function simply outputs a nil value to the outside of the function. What is going on here?
You're just doing getNumbers() to recurse instead of return getNumbers(). This means that if the recursion gets entered, the final returned value will be nil no matter what else happens.
In the else case of the if valid then, you are not returning anything. You only return anything in the valid case. In the else case, a recursive call may return something, but then you ignore that returned value. The print you see is corresponding to the return from the recursive call; it isn't making it out the original call.
You mean to return getNumbers().
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)
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"}
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