解压缩功能问题在Lua

我有以下unpack()函数:

function unpack(t,i)  
    i = i or 1  
    if t[i] then  
        return t[i],unpack(t,i + 1)  
    end  
end

我现在在以下测试代码中使用它:

t = {"one","two","three"}  
print (unpack(t))  
print (type(unpack(t)))  
print (string.find(unpack(t),"one"))  
print (string.find(unpack(t),"two"))

哪个输出:

one two three  
string  
1   3  
nil

令我困惑的是最后一行,为什么结果为零?

解决方法

如果函数返回多个值,除非将其用作最后一个参数,否则仅采用第一个值.

在你的例子中,string.find(unpack(t),“one”)和string.find(unpack(t),“two”),“two”和“three”被丢弃,它们相当于:

string.find("one","one")  --3

string.find("one","two")  --nil

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...