mIRC Homepage
Posted By: beer help evaluating dynamic variable names - 05/04/20 11:53 PM
I'm having trouble evaluating a variable where part of the name is dynamic. Broken example:
Code
var %test.playcount = 1, %test.pausecount = 2, %test.reccount = 3
var %which = pause
var %output = %test. $+ [ %which ] $+ count

I expected %output to be set to the contents of %test.pausecount ("2") but if I echo %output, I just get "pausecount". I've tried "var %output = % $+ test. $+ %which $+ count" and other variations but have yet to stumble across the secret sauce that gives me the result I'm looking for.

Any help is greatly appreciated!
Posted By: Wims Re: help evaluating dynamic variable names - 06/04/20 04:05 AM
https://en.wikichip.org/wiki/mirc/variables#Dynamic_Variable_Names
Posted By: beer Re: help evaluating dynamic variable names - 06/04/20 03:25 PM
I've already read that and it doesn't address my question, hence why I've asked here. Whether the answer is that dynamically named variables can't be formatted the way I'm doing it, or there's a problem with how I'm trying populating it, the link lacks clarity on that.
Posted By: Wims Re: help evaluating dynamic variable names - 06/04/20 04:28 PM
Originally Posted by https://en.wikichip.org/wiki/mirc/variables#Dynamic_Variable_Names
You can also get the value of a dynamic variable by using $eval, $eval allows you to force an expression to evaluate more than once, a bit like the brackets [ ], but brackets are meant to alter the order of evaluation of a line, which itself can have its own drawbacks.

$eval($+(%,<static>,<dynamic>),2)
So you would use $eval($+(%,test.,%which,count),2)

Bracket evaluation are more complex to understand, people usually get away with one single dynamic part, but in your case you kind of have two with the hardcoded 'count'

If you start with %test., you have to follow with [ $+ [ dynamic ] $+ count ]
But if count were a variable such as %count, this wouldn't work and you have to add another pair of $+ [ ]:

Code
//var %test.pausecount 2,%which pause,%count count | echo -a %test. [ $+ [ %which $+ [ %count ] ] ]
I'll change wikichip to be clearer regarding that, but the bracket evaluation page, which is linked in the article for dynamic variable, explains this all.
Posted By: kap Re: help evaluating dynamic variable names - 06/04/20 04:37 PM
Code
alias t30 {
  var -s %test.playcount = 1, %test.pausecount = 2, %test.reccount = 3
  var -s %which = pause
  var -s %output = %test. [ $+ [ %which ] $+ count ]
}
Posted By: beer Re: help evaluating dynamic variable names - 07/04/20 02:02 AM
Thank you for clarifying this! The examples given here make it much easier to understand. smile
© mIRC Discussion Forums