Problems with numbers

Posted by Edgeofforever on Fri 26 Nov 2004 05:02 PM — 3 posts, 13,670 views.

#0
I'm having a problem with a script I've been writing.
I use the following trigger to get a number in representing the number of a certain potion in a container.

^\s*(a|an)\s*(sanctuary|bless|fly|armor|protection|true|create|alertness|dispel|grounding|heal|cure|empty)\s*(blindness|poison|magic|spring|sight|)\s*(potion|flask)\s*\(\s*(\d+)\s*\)\s*$

It sends the number to a script and puts it into a variable. I created another variable:

maxLong = 1500

Then later in the scrip I compare the two like this:

if sanctuary < max long then

Where sanctuary is the number brought in from the trigger. Even when sanctuary is clearly less then 1500, it doesn't go through the if code. Can anyone help?
Australia Forum Administrator #1
This is the classic problem of confusing numbers with strings. We can reproduce it in the Immediate window like this:


SetVariable "sanctuary", 22
SetVariable "maxlong", 1500

sanctuary = GetVariable ("sanctuary")
maxLong = GetVariable ("maxlong")

if sanctuary  < maxLong then
  Note "Pass"
else
  Note "Fail"
end if


Run this, and it prints "Fail".

This is because it compares "22" to "1500" and thinks 22 is higher, which it is as a string. This works, converting both numbers to integer:


sanctuary = CInt (GetVariable ("sanctuary"))
maxLong = CInt (GetVariable ("maxlong"))



Other languages would have similar syntax. For example in Lua you need to use "tonumber" to do the conversion.
#2
Perfect, thanks.