'Problem with Load function in Lua while execution
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Egor Skriptunoff |
