I have the following unpack() function:
function unpack(t, i)
i = i or 1
if t[i] then
return t[i], unpack(t, i + 1)
end
end
I now use it in the following test code:
t = {"one", "two", "three"}
print (unpack(t))
print (type(unpack(t)))
print (string.find(unpack(t), "one"))
print (string.find(unpack(t), "two"))
which outputs:
one two three
string
1 3
nil
What puzzles me is the last line, why is the result nil?
If a function returns multiple values, unless it's used as the last parameter, only the first value is taken.
In your example, string.find(unpack(t), "one") and string.find(unpack(t), "two"), "two" and "three" are thrown away, they are equivalent to:
string.find("one", "one") --3
and
string.find("one", "two") --nil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With