Search code examples
luabooleanruntime-errorcomparison-operators

Lua Boolean compound or


I'm just getting started with a basic Lua interpreter, and I've run into this issue:

a = tonumber(a)
b = tonumber(b)
if a < 1 or b < 1 or a > x or b > x then
    ...
end

And I'm getting an error message:

Attempt to compare number with string expected, got number

I'm new to this. How should that statement be written in Lua?

EDIT: x is set as one of the input arguments. I'm positive that it's a number, not a string; in previous statements I used it as a maximum value in a for loop.


Solution

  • x is set as one of the input arguments. I'm positive that it's a number, not a string; in previous statements I used it as a maximum value in a for loop.

    The input arguments (from arg) are always strings, which will be coerced into numbers in a for loop:

    for i = 1, "4" do print(i) end
    -- 1
    -- 2
    -- 3
    -- 4
    

    In this case, though, it looks like you'll need to use tonumber().