Lua中如何实现长尾词的解压缩功能?

2026-03-31 22:391阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计252个文字,预计阅读时间需要2分钟。

Lua中如何实现长尾词的解压缩功能?

我有一个如下`unpack`函数:luafunction unpack(t, i) i=i or 1 return i <=#t and t[i] or return t[i], unpack(t, i + 1)end我现在在以下测试代码中使用它:luat={one, two, three}print(unpack(t))print(type(unpack(t)))print(str(unpack(t)))

我有以下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

Lua中如何实现长尾词的解压缩功能?

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

本文共计252个文字,预计阅读时间需要2分钟。

Lua中如何实现长尾词的解压缩功能?

我有一个如下`unpack`函数:luafunction unpack(t, i) i=i or 1 return i <=#t and t[i] or return t[i], unpack(t, i + 1)end我现在在以下测试代码中使用它:luat={one, two, three}print(unpack(t))print(type(unpack(t)))print(str(unpack(t)))

我有以下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

Lua中如何实现长尾词的解压缩功能?

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