Comparing 2 variables became variable and string

Post Reply
lunloon
Posts: 9
Joined: Mon Feb 06, 2017 4:55 am

Comparing 2 variables became variable and string

Post by lunloon »

I have a trigger with the following Lua:

abi=matches[2]
bdee=matches[3]
echo(abi)
echo(bdee)

if abi<bdee then echo("yay") end

I could see the echos so i know my captures were correct (both are numbers), but when i try to do the a<b, it says in the debug about trying to compare a number with a string.

How do I make it know that bdee is not a string but the number that i captured?

Drevarr
Posts: 43
Joined: Tue Aug 06, 2013 12:07 am
Location: GA, USA

Re: Comparing 2 variables became variable and string

Post by Drevarr »

tonumber( )
Code: [show] | [select all] lua
abi=tonumber(matches[2])
bdee=tonumber(matches[3])
echo(abi)
echo(bdee)

lunloon
Posts: 9
Joined: Mon Feb 06, 2017 4:55 am

Re: Comparing 2 variables became variable and string

Post by lunloon »

i've tried that actually, it's the line

if abi<bdee then echo("yay") end

that doesn't work. Even if I had tonumber the 2 variables it still tells me i am comparing a number to a string.

User avatar
SlySven
Posts: 1019
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Comparing 2 variables became variable and string

Post by SlySven »

Perhaps lua is helpfully transforming the variables just at the wrong time - how about the more explicit:

Code: Select all

abi=matches[2]
bdee=matches[3]
echo(abi)
echo(bdee)

if tonumber(abi) < tonumber(bdee) then echo("yay") end
It might be that the action of echo ing the variables is turning one of them back to a string internally - especially if the variable has a space (decimal point a.k.a. a full stop) or something else that might appear near a number without actually making it NOT a number?

lunloon
Posts: 9
Joined: Mon Feb 06, 2017 4:55 am

Re: Comparing 2 variables became variable and string

Post by lunloon »

SlySven wrote:Perhaps lua is helpfully transforming the variables just at the wrong time - how about the more explicit:

Code: Select all

abi=matches[2]
bdee=matches[3]
echo(abi)
echo(bdee)

if tonumber(abi) < tonumber(bdee) then echo("yay") end
It might be that the action of echo ing the variables is turning one of them back to a string internally - especially if the variable has a space (decimal point a.k.a. a full stop) or something else that might appear near a number without actually making it NOT a number?
I didn't have that echo() with the if then function at the same time, i tested the captures first using the echo, and i replaced the echo functions with the if then function.

And i also did that tonumber(abi) < tonumber(bdee) as well

lunloon
Posts: 9
Joined: Mon Feb 06, 2017 4:55 am

Re: Comparing 2 variables became variable and string

Post by lunloon »

I am sorry. It works now.
I just cut and pasted the same code and it works now.

No idea why. :(

edited: the same code is the one with the tonumber()<tonumber() to clarify. I must have left something somewhere that got cut away. Hopefully.

Thank you all for the help.

Post Reply