Variables using text

Posted by Terry on Wed 30 Jan 2008 12:06 PM — 7 posts, 33,612 views.

USA #0
There have been a few times that I've tried to use a wildcard which picked up text. However, whenever I've tried to use the corresponding variable, MUSHclient always said something about comparing numbers and text. Is there a way to use text in ifchecks?
Germany #1
Can you provide us with a short example, e.g. using the plugin wizard to extract one trigger with the code you try to use?
Australia Forum Administrator #2
Here is an example of the error:


print ("5" > 6)   --> Error:  attempt to compare number with string


You can convert strings to numbers. Wildcards in triggers and aliases are returned as strings, so they must be converted under certain circumstances.


print (tonumber ("5") > 6) --> false



In some cases Lua can deduce what is required by the operator, eg:


print ("5" + 2) --> 7
USA #3
Words of warning: Make sure to check for sane values before converting a string into a number. Ordinarily, coercion is automatic, so if your script is throwing an error, it might just be due to bad data.
Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> =tonumber( 9 )
9
> =tonumber( "9" )
9
> =tonumber( "F" )
nil
> =tonumber( "1,000" )
nil
> =tonumber( nil )
nil
> = tonumber( "F" ) > 9
stdin:1: attempt to compare number with nil
stack traceback:
        stdin:1: in main chunk
        [C]: ?
>
Australia Forum Administrator #4
Agreed.

However if you use a regular expression in your trigger, and make sure that the wildcard in question must be a sequence of numeric digits, then you have eliminated that problem.
USA #5
I was talking about having strings compared to strings. Like if %1 = "billy" or something. Is that possible?
Australia Forum Administrator #6
See http://www.mushclient.com/faq point 32.