【问题标题】:Using a variable as arithmetic operator in Lua在 Lua 中使用变量作为算术运算符
【发布时间】:2021-04-21 03:01:27
【问题描述】:

我想在if 语句表达式中使用一个引用算术运算符的变量,如下所示:

str = { '>60', '>60', '>-60', '=0' }
del = 75

function decode_prog(var1, var2)
    op = string.sub(var1, 1, 1)
    vb = tonumber(string.sub(var1, 2, 3))

    if var2 op vb then
        print("condition met")
    else 
        print('condition not meet')
    end
end
for i = 1, #str do
    decode_prog(str[i], del)
end

当上面的代码执行时,它应该根据操作的结果打印 "condition met""condition not met",但是我却收到了一个错误。

【问题讨论】:

  • 你应该在你的问题中包含你得到的具体错误

标签: lua lua-table


【解决方案1】:

您不能用引用函数的变量替换原生 Lua 运算符,唯一的方法是在关联数组中创建一组函数并将索引设置为对您要执行的相应操作。

查看您的列表,您有一个大于 (>) 且等于 (=)。我们为这些操作创建一个表,该表采用如下两个参数。

local operators = {
    [">"] = function(x, y) return x > y end,
    ["="] = function(x, y) return x == y end,
    -- Add more operations as required.
}

然后,您可以通过从字符串中获取操作字符以及数值本身来从 decode_prog 函数调用相应的函数 - 这是可能的,因为您可以从索引为的关联数组中获取函数我们要执行的操作的字符串。

local result = operators[op](var2, number)

这会调用operators 数组,使用op 来确定我们需要转到哪个索引以进行适当的操作,并返回值。

最终代码:

str = { '>60', '>60', '>-60', '=0' }
del = 75

local operators = {
    [">"] = function(x, y) return x > y end,
    ["="] = function(x, y) return x == y end,
}

function decode_prog(var1, var2)
    local op = string.sub(var1, 1, 1) -- Fetch the arithmetic operator we intend to use.
    local number = tonumber(string.sub(var1, 2)) -- Strip the operator from the number string and convert the result to a numeric value.

    local result = operators[op](var2, number) -- Invoke the respective function from the operators table based on what character we see at position one.

    if result then
        print("condition met")
    else 
        print('condition not meet')
    end
end

for i = 1, #str do
    decode_prog(str[i], del)
end

【讨论】:

  • >- 只是大于负 60,因为运算符是 string.sub(_, 1, 1)
  • 否则,这个解决方案比我的要优雅得多。即使您支持数百种不同的运算符,代码也非常清楚它的作用。
  • 感谢您对>- 的澄清,这意味着否定!
  • 还可以考虑使用local number = tonumber(string.sub(var1, 2)) 来捕获整个字符串的其余部分。否则,您将得到 op == ">"number == -6 而不是 -60。
【解决方案2】:

我无法理解您的代码或您想要实现的目标,但如果可以简单地使用load。 您将表达式构建为字符串并运行它。当然,您应该注意两个字符运算符,例如 >=,而我没有这样做,并且您应该验证您的输入。

local str={'>60','>60','>-60','=0'}
local del=75

function decode_prog(var1, var2)
  local operator = var1:sub(1,1):gsub("=", "==")
  local expr = string.format("return %d %s %s", var2,operator, var1:sub(2))
  print(string.format("condition %smet", load(expr)() and "" or "not "))
end

for i,v in ipairs(str) do
  decode_prog(v, del)
end

【讨论】:

  • 我其实对性能很好奇。与每个运算符的函数数组相比,您能说出负载的执行情况吗?这个解决方案非常好,无需任何额外编码即可解析任何运算符,但我一直认为这种load() 用法不是最好的想法,但我从未想过要研究它。
  • @IsawU 我不知道。使用数组应该更快。但是这个特殊的任务看起来不像是性能很重要的事情。
【解决方案3】:

一个非常简单的方法是为每个支持的运算符添加一个条件:

function decode_prog(var1, var2)
  op = string.sub(var1, 1, 1)
  vb = tonumber(string.sub(var1, 2))  --remove the last argument and use tonumber()
  if vb == nil then return end  --if the string does not contain number

  if (op == ">" and var2 > vb) or (op == "=" and var2 == vb) --[[add more conditions here]] then
     print("condition met")
  else 
     print("condition not met")
  end
end

我也更改了vb=string.sub(var1,2,3) 行。 这种形式vb = tonumber(string.sub(var1, 2)) 将允许使用具有任意位数的数字并添加tonumber(),这将允许我们在比较可能失败时捕获非数字错误。

然后我添加了一个逻辑来确定运算符是什么以及是否满足条件。

运营商限制: 这仅适用于只有一个字符的运算符,而>= 等运算符将无法使用,除非您使用不同的字符。 不会很好玩,因为它是多个字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多