Strange logic in a Lua script?

I cannot seem that Lua is evaluating booleans.

Here is a trivial snippet designed to demonstrate the problem:

function foo()
  return true
end

function gentest()
   return 41
end

function print_hello()
  print ('Hello')
end


idx = 0

while (idx < 10) do
 if foo() then
    if (not gentest() == 42) then
       print_hello()
    end
 end
 idx = idx +1
end

When this script is run, I expect to see "Hello" printed on the console, but nothing is printed. Can someone explain this?

+3
source share
4 answers

Inside the while loop, you should use notoutside the brackets:

while (idx < 10) do
 if foo() then
    if not (gentest() == 42) then
       print_hello()
    end
 end
 idx = idx +1
end

(gentest() == 42)will return false, then not falsewill return true.

(not gentest() == 42)matches with ( (not gentest()) == 42). Since not gentest()returns not 41== false, you will receive false == 42and finally return false.

+10
source

Try it not (gentest() == 42)..

+2
source

, , not , ==,

if ((not 41) == 42) then

... , , - ( ) 42.

+1

"" , . , - " a" , , , , "a", " a" , , , , 'a' . , "" , , "if a not = 42", , .

0

Source: https://habr.com/ru/post/1792259/


All Articles