【问题标题】:Cycling through different file location options循环浏览不同的文件位置选项
【发布时间】:2019-06-18 17:00:51
【问题描述】:

我实际上是在尝试创建一个函数来测试我给出的第一个位置,格式如下:

 myComputer.referenceLookup("/address/x/text")

如果它不是 NULL 或“None”或“”(空),则返回该位置的字符串。

如果没有,我希望它测试下一个可能的位置:

 myComputer.referenceLookup("/address/1/x/text")

否则,我希望它返回一个空字符串 ("")。

我尝试查看 Lua Manual 并在 repl.it 中测试不同的表单无济于事,但不幸的是,我无法像通常在测试时那样复制类似的示例。

function firstLine(x)

if myComputer.referenceLookup("/Address/ .. (x) .. /text") != NULL or "None" or "" then

    return myComputer.referenceLookup("/Address/ .. (x) .. /text")

elseif myComputer.referenceLookup("/Address/1/ .. (x) .. /text") !=  NULL or "None" or "" then

    return myComputer.referenceLookup("/Address/1/ .. (x) .. /text")

else

    return ""

end

end

myComputer.out.firstHouseNumber = firstLine(housenumber)

值得注意的是,我引用事实的通常方式如下:

myComputer.out.firstHouseNumber= myComputer.referenceLookup("/Address/housenumber/text")

myComputer.out.firstHouseNumber= myComputer.referenceLookup("/Address/1/housenumber/text")

我使用的平台不会抛出错误,它只会返回空白而不是运行 lua 脚本,因此我无法调试(因此通常使用 repl.it)。

我知道这使它成为一个抽象的问题,但如果有人知道我如何做我所描述的事情,将不胜感激。

【问题讨论】:

  • 当您说return the string in that location 时,您的意思是您要阅读文本文件并返回内容吗?还是只返回经过验证的路径? - 关于您的问题的旁注:firstLine 的 lua 示例不是有效的 lua。使用NULL 而不是nil!= 而不是~="/Address/ .. (x) .. /text" 不会达到预期的效果(它缺少一些额外的"
  • @Nifim,理想情况下,返回该位置的字符串,我关于 != 和 ~= 的错误,我一直在从系统使用的 DSL 转换为 Lua 并且令人困惑, ~= 用作大约。相等!

标签: lua lookup file-location


【解决方案1】:

假设

看你的回答,我会假设

  1. myComputer.referenceLookup 在其他地方定义并按预期工作(而不是这个问题的一部分)
  2. NULL 也在其他地方定义,代表某种零值

回答

线

if myComputer.referenceLookup("/Address/ .. (x) .. /text") != NULL or "None" or "" then

不起作用,因为 or 运算符不能那样工作。

Lua 是如何解释的

if (myComputer.referenceLookup("/Address/ .. (x) .. /text") != NULL) or "None" or ""

并且由于“None”是一个字符串值,因此被认为是真实的,if 条件将始终评估为 true,因此它将始终返回第一个位置。此外,Lua 中没有 != 运算符;而是~=

至于解决方案,您基本上需要像这样的三个比较:

if myComputer.referenceLookup("/Address/" .. x .. "/text") ~= NULL
and myComputer.referenceLookup("/Address/" .. x .. "/text") ~= "None"
and myComputer.referenceLookup("/Address/" .. x .. "/text") ~= "" then

显然三次调用函数是个坏主意,既因为性能,也因为它可能有副作用,所以最好先将它保存到一个变量中,如下所示:

local result = myComputer.referenceLookup("/Address/" .. (x) .. "/text")
if result ~= NULL and result  ~= "None" and result  ~= "" then
  return result
end

额外

如果您想让您的程序更容易扩展,您还可以使用string.format 从模板构建位置。假设您有一个包含所有位置的表格,如下所示:

local locations = {
  "/Address/%s/text";
  "/Address/1/%s/text";
}

然后您可以使用ipairs 遍历条目并使用string.format 构建每个位置:

for index, template in ipairs(locations) do
  local result = myComputer.referenceLookup(template:format(x))
  if result ~= NULL and result  ~= "None" and result  ~= "" then
    return result
  end
end

请注意,只要模板是字符串,您就可以将string.format(template, x) 写为template:format(x)。 (further reading)

【讨论】:

  • 非常感谢!我关于 != 和 ~= 的错误,我一直在从系统使用的 DSL 转换为 Lua 并且令人困惑的是, ~= 被用作大约。平等的!额外的帮助也非常感谢。
  • 对不起,还有一个问题!该文件的位置是:myComputer.referenceLookup("/Address/example/text") 而不是 myComputer.referenceLookup(/Address/example/text)。 IE。位置周围的引号。我想知道在这种情况下它是如何工作的
  • @Bee 我不太明白你的意思,抱歉。没有引号的例子根本不是有效的 Lua 语法。
  • 这基本上就是我要说的,如果你像上面那样在括号内连接位,它还会返回一个字符串并因此返回一个有效地址吗?我从您的回答中认为是的,我对 Lua 还很陌生,所以只是想了解一下!
  • @Bee 简短回答:"hello" .. "world""helloworld" 完全相同,绝对没有区别。长答案:Official Reference, 3.4.6 – Concatenation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2010-12-20
  • 1970-01-01
相关资源
最近更新 更多