Gammon Forum
Entire forum
MUSHclient
VBscript
Problems with numbers
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Posted by
| Edgeofforever
(25 posts) bio
|
Date
| Fri 26 Nov 2004 05:02 PM (UTC) |
Message
| 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? | top |
|
Posted by
| Nick Gammon
Australia (22,975 posts) bio
Forum Administrator |
Date
| Reply #1 on Fri 26 Nov 2004 08:20 PM (UTC) |
Message
| 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.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
9,605 views.
It is now over 60 days since the last post. This thread is closed.
  Refresh page
top
Quick links:
MUSHclient.
MUSHclient help.
Forum shortcuts.
Posting templates.
Lua modules.
Lua documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.