This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ardera's profile picture

[LUA] Attempt to compare boolean with number?

Started by ardera, 13 June 2012 - 02:20 PM
ardera #1
Posted 13 June 2012 - 04:20 PM
I am programming on a server and a browser (with a whole team) , and now im working on the browser… In this function:

function hasAllStructures(theTable)
    counter2=0
foundVariablesDeclaration=false
foundVariablesDeclarationEnd=false
foundOutprint=false
foundOutprintEnd=false
tablemaxn=table.maxn(theTable)
while true do
   counter2=counter2+1
   print(counter2.." "..tablemaxn)
   if not counter2>tablemaxn then
	    --coarse search for errors
  if theTable[counter2]=="VARIABLES-DECLARATION" then
    foundVariablesDeclaration=true
  elseif theTable[counter2]=="VARIABLES-DECLARATION-END" then
    foundVariablesDeclarationEnd=true
  elseif theTable[counter2]=="PRINT-OUT" then
    foundOutprint=true
  elseif theTable[counter2]=="PRINT-OUT-END" then
    foundOutprintEnd=true
  end
  if counter2==table.maxn(theTable) then
    break
  end
   else
	 if counter2==1 then
    return false, "01"
  else
    return false, "02"
  end
   end
end
--return
if foundVariablesDeclaration~=true then
   return false, "03"
elseif foundVariablesDeclarationEnd~=true then
   return false, "04"
elseif foundOutprint~=true then
   return false, "05"
elseif foundOutprintEnd~=true then
   return false, "06"
else
   return true
end
end

its coarse searching for errors, but in one error detecting ( line 11 ) it says: attempt to compare boolean with number
so i put in a print command, and it prints 0 and 2, but the program says 0 (counter2) is a boolean! I don't know why, but I think its a bug, and I don't know how to repair it…
I'm from Germany, and my english skills are bad, so sometimes I had to use the Google translator :(/>/>
MysticT #2
Posted 13 June 2012 - 05:31 PM
Well, you are converting it to a boolean with the not:

if not counter2>tablemaxn then
It's trying to compare the result of "not counter2" (wich is a boolean) with "tablemaxn" (a number). It should be:


if not (counter2 > tablemaxn) then
Or even better:


if counter2 <= tablemaxn then
ardera #3
Posted 13 June 2012 - 06:22 PM
oh thanks i did not know that "not" is only for booleans :(/>/> thanks
MysticT #4
Posted 13 June 2012 - 06:41 PM
Actually, not can be used with any value. It's an unary operator (has only one operand), wich evaluates the value and returns the opposite boolean.
Lua evaluates values like this:
- nil returns false
- booleans return it's value
- other values return true
It would be something like:

function evaluate(value)
  if type(value) == "nil" then
	return false
  elseif type(value) == "boolean" then
	return value
  else
	return true
  end
end
And not just returns the opposite (false when it's true, and true when it's false).

Your problem was just about the order of the operations. It evaluated the first variable (wich returns a boolean), and then compared with the other one.