mIRC Home    About    Download    Register    News    Help

Print Thread
#8434 26/01/03 05:31 PM
Joined: Dec 2002
Posts: 25
S
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Dec 2002
Posts: 25
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

Last edited by SubSpace; 26/01/03 05:31 PM.
#8435 26/01/03 07:59 PM
Joined: Dec 2002
Posts: 1,321
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Dec 2002
Posts: 1,321
var %change = $calc($2 - $eval($+(%,bonus_,$1),2))


DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C
#8436 27/01/03 05:44 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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 ] ] )

Last edited by qwerty; 27/01/03 05:47 AM.

/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#8437 27/01/03 09:14 AM
Joined: Dec 2002
Posts: 25
S
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Dec 2002
Posts: 25
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


Link Copied to Clipboard