Lua中Load函数执行时出现问题

发布于 2025-01-11 21:08:39 字数 483 浏览 0 评论 0原文

print("i", "j", "i & j")
for i = 0,1 do
   for j=0,1 do
   print(i, j, i & j)
   end
end

上面的代码在Lua中运行良好。它给出以下输出。

i   j   i & j
0   0   0
0   1   0
1   0   0
1   1   1

但是下面的代码不起作用。基本上我想根据一些用户输入生成真值表。加载功能似乎有问题。可能与函数加载处理的变量类型有关。有人可以建议吗?我们可以编写Lua函数来实现同样的功能吗?这是不起作用的代码。

str="i & j"
print("i", "j", "i & j")
for i = 0,1 do
    for j=0,1 do
        print(i,j,load(str))
    end
end
print("i", "j", "i & j")
for i = 0,1 do
   for j=0,1 do
   print(i, j, i & j)
   end
end

The above code works fine in Lua. It gives the following output.

i   j   i & j
0   0   0
0   1   0
1   0   0
1   1   1

However the following code doesn't work. Basically I want to generate Truth Table based on some user input. There seems to be issue with load function. Probably it is related with type of variable that function load handles. Can anyone suggest? Can we write Lua function to achieve same? Here is the code that doesn't work.

str="i & j"
print("i", "j", "i & j")
for i = 0,1 do
    for j=0,1 do
        print(i,j,load(str))
    end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎的声音 2025-01-18 21:08:39

这是一个应该最多可处理近六十个值的尝试:

-- Lua >= 5.3
local concat = table.concat

local str    = "((i | j) ~ k) & l" -- io.read() ?
local sep    = " "

local values = {} -- 0 or 1
-- i, j, k, etc.
local names  = setmetatable({},
  {__index = function(t,k)
    t[k] = 0
    values[#values+1] = k
    return 0
  end}
)

local aux = {} -- Values to be printed
load("return " .. str,nil,"t",names)()  -- Initialization
local n = #values
print(concat(values,sep) .. sep .. str) -- Variables
for i = 0,(1<<n)-1 do -- 2^n evaluations
  local k = i
  for j = n,1,-1 do
    names[values[j]] = k % 2
    aux[j] = k % 2
    k = k >> 1
  end
  print(concat(aux,sep) .. sep .. load("return " .. str,nil,"t",names)())
end
i j k l ((i | j) ~ k) & l
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 1
0 1 0 0 0
0 1 0 1 1
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 1
1 0 1 0 0
1 0 1 1 0
1 1 0 0 0
1 1 0 1 1
1 1 1 0 0
1 1 1 1 0

Here's an attempt which is supposed to work with a maximum of nearly sixty values:

-- Lua >= 5.3
local concat = table.concat

local str    = "((i | j) ~ k) & l" -- io.read() ?
local sep    = " "

local values = {} -- 0 or 1
-- i, j, k, etc.
local names  = setmetatable({},
  {__index = function(t,k)
    t[k] = 0
    values[#values+1] = k
    return 0
  end}
)

local aux = {} -- Values to be printed
load("return " .. str,nil,"t",names)()  -- Initialization
local n = #values
print(concat(values,sep) .. sep .. str) -- Variables
for i = 0,(1<<n)-1 do -- 2^n evaluations
  local k = i
  for j = n,1,-1 do
    names[values[j]] = k % 2
    aux[j] = k % 2
    k = k >> 1
  end
  print(concat(aux,sep) .. sep .. load("return " .. str,nil,"t",names)())
end
i j k l ((i | j) ~ k) & l
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 1
0 1 0 0 0
0 1 0 1 1
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 1
1 0 1 0 0
1 0 1 1 0
1 1 0 0 0
1 1 0 1 1
1 1 1 0 0
1 1 1 1 0
想念有你 2025-01-18 21:08:39

使用小技巧:

local str="i * j" -- or i & j for v5.3 lua
print("i", "j", str)
for i = 0,1 do
    for j=0,1 do
       local  s = str:gsub("i",i):gsub("j",j)
       print(i,j, (loadstring or load)("return "..s)() )
    end
end

use little tricks:

local str="i * j" -- or i & j for v5.3 lua
print("i", "j", str)
for i = 0,1 do
    for j=0,1 do
       local  s = str:gsub("i",i):gsub("j",j)
       print(i,j, (loadstring or load)("return "..s)() )
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文