|
Ice_Aziraphale
|
Ice_Aziraphale
|
Hi, I'm having some problems using $calc to work out an average for me. The code I'm using is:
msg #You have got %TB.Correct. [ $+ [ $address($nick,4) ] ] $&
correct out of %TB.Goes. [ $+ [ $address($nick,4) ] ] tries.$&
This is from a total of %TB.Wires. [ $+ [ $address($nick,4) ] ] wires,$&
with an average of$&
$calc(%TB.Wires. [ $+ [ $address($nick,4) ] ] / %TB.Goes. [ $+ [ $address($nick,4) ] ]) wires per go.
This code correctly sends the numbers in the first half (or as I suspect, the text version of the numbers), but the $calc returns a 0 everytime. When doing some debugging by getting the code to msg just values such as %TB.Wires. [ $+ [ $address($nick,4) ] ] and $calc(%TB.Wires. [ $+ [ $address($nick,4) ] ]) , I've found that the variables return as numbers (or text) but if placed inside a $calc, they return as 0. Any thoughts?
|
|
|
|
Joined: Mar 2005
Posts: 420
Fjord artisan
|
Fjord artisan
Joined: Mar 2005
Posts: 420 |
I've found that the variables return as numbers (or text) but if placed inside a $calc, they return as 0. Any thoughts? If you're trying to divide numbers & text, it will return 0.
|
|
|
|
Joined: Dec 2002
Posts: 2,884
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,884 |
The problem is your closing evaluation bracket is touching the closing parenthesis of the $calc() identifier. $calc(%TB.Wires. [ $+ [ $address($nick,4) ] ] / %TB.Goes. [ $+ [ $address($nick,4) ] [color:red]])[/color] should be: $calc(%TB.Wires. [ $+ [ $address($nick,4) ] ] / %TB.Goes. [ $+ [ $address($nick,4) ] [color:red]] )[/color] (highlighted in red around where the space is).
|
|
|
|
SCNDRL
|
SCNDRL
|
if you use $eval or $() you may not mix Click
|
|
|
|
DaveC
|
DaveC
|
Its already been pointed out about $eval() or $() to u, but i thought id actually show u the code, and explain what you can do, in the long run its far better to use $eval() or $() as using [ [ ] ] has some dramatic side/unexpected effects in some events.
msg #You have got $($+(%,TB.Correct.,$address($nick,4)),2) $&
correct out of $($+(%,TB.Goes.,$address($nick,4)),2) tries.$&
This is from a total of $($+(%,TB.Wires.,$address($nick,4)),2) wires,$&
with an average of$&
$calc($($+(%,TB.Wires.,$address($nick,4)),2) / $($+(%,TB.Goes.,$address($nick,4)),2)) wires per go. What i have done is a 2 step thing (1) create the variable name $+(%,TB.XXXX,$address($nick,4)) the concaternating of the parts, not the leading % is seprated to stop mirc attempting to use the contents of %TB.XXXX (2) now i have the variable name i tell mirc to evaluate it 2 times (the first time would have been the concaternating of the $+ parts, so $() becomes %TB.XXXX. $address($nick,4) and then becomes the contents of it. ex var $+(%,TB.XXXX,$address($nick,4)) = 12345 //echo -a $( $+(%,TB.XXXX,$address($nick,4)) ,2) * I have found the more complexe you start making your variables the harder it becomes to use [ [ ] ] while the simplier it becomes to use $() Amagine if u had to become network and channel and nick specific %TB.XXXX. [ $+ [ $network ] $+ ] . [ $+ [ $chan ] $+ ] . [ $+ [ $address($nick,4)) ] ]vs $+(%,TB.XXXX.,$network,.,$chan,.,$address($nick,4))
|
|
|
|
Joined: Dec 2002
Posts: 2,884
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,884 |
I wouldn't advise people to use $(). It's extremely bad form to use a non-descriptive identifier like $() when $eval() is available to do the same thing. Especially since it's not even documented that $() will work outside of event declarations. It makes for poor readability of code and it's difficult for new scripters to understand.
|
|
|
|
Joined: Mar 2005
Posts: 420
Fjord artisan
|
Fjord artisan
Joined: Mar 2005
Posts: 420 |
I disagree on that. It would be better to let new scripters know that such identifier exists and that '$()' & '$eval()' works the same. Yes, $() works outside events, just like $eval does.
|
|
|
|
DaveC
|
DaveC
|
Agreed it would have been better had i used $eval() and just mentioned that this could be shortened to $() but with the cavet of its undocumented.
|
|
|
|
Joined: Dec 2002
Posts: 2,884
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,884 |
But as it's completely undocumented behaviour there's no guarantee that it will continue to work outside events in future versions of mIRC (although admittedly it's probably unlikely to be removed now for the sake of it). More importantly though, using $() creates non-descriptive code, which is bad practice in general. This becomes particularly apparent when used in conjunction with $+(), which it almost always is in dynamic variable situations such as the one described in this thread, because the two identifiers look so similar and is a common cause of typos going unnoticed.
Hardly worth saving 4 bytes over is it?
|
|
|
|
DaveC
|
DaveC
|
but $( is so much easier to type than $eval( <snicker snicker>
|
|
|
|
Ice_Aziraphale
|
Ice_Aziraphale
|
Thank you all. Adding the space between the "]" and the ")" worked and I'll look into changing all my code to use the $() or $eval.
|
|
|
|
|