mIRC Homepage
Posted By: SubSpace $calc strangeness? - 26/01/03 05:31 PM
I ran into some $calc behaviour I find rather odd, and I was hoping someone here could explain why it happens. Usually when doing calculations I use $calc to avoid things being interpreted as text instead. Now consider this line:

Code:
var %change = $calc($2 - %bonus_ [ $+ [ $1 ] ])


$1 = str (the rest of the variable name I want to use..)
$2 = 0
%bonus_str = 4

%change becomes 0, while I was expecting 0 - 4 = -4
If I omit the $calc() part, %change is correctly assigned the value -4, but why?

Explanation would be appreciated to prevent future potential bugs in my scripts smile
Posted By: Hammer Re: $calc strangeness? - 26/01/03 07:59 PM
var %change = $calc($2 - $eval($+(%,bonus_,$1),2))
Posted By: qwerty Re: $calc strangeness? - 27/01/03 05:44 AM
The problem is that the rightmost square bracket touches $calc's closing bracket. Evaluation brackets must not touch anything else otherwise they lose their special meaning. Try this:
Code:
var %change = $calc($2 - %bonus_ [ $+ [ $1 ] ] )
Posted By: SubSpace Re: $calc strangeness? - 27/01/03 09:14 AM
Hmm, outside too huh? All that seemingly unnecessary whitespace...

Anyway, thanks smile
I'm sure Hammer's $eval solution works, but I haven't decided yet which I prefer.. It gets rid of some nested brackets, but the alternative isn't much better.

I think I'll take my chances with some extra whitespace for now smile
© mIRC Discussion Forums