mIRC ScriptBox
Arrays And "Dynamic" Variable Techniques

Written by Ntd

1. Arrays And "Dynamic" Variables

Some very common problems are

How do I make an array of variables?
How do I set and retrieve a variable for any nick?

Well, this information is in the mIRC help file, but it's extremely well hidden!

  /say % [ $+ [ $1 ] ] 

There it is, the big secret. It may not look like much, but in fact its the basis of arrays (and the related topic of storing and looking up stuff for each nick.)

Sadly, Khaled stopped short of explaining how that code is any use, so here goes. What the code above does is to stick a % sign on front of whatever $1 is to get a variable name, re-evaluate it and then /say it. So if $1 was "test", mIRC would say whatever was in %test. It would not say "%test"!! (That's important.)
Try this from any editbox:

  /set %message hello 
  /set %test message 
  //echo -a % [ $+ [ %test ] ]
The double slash there is important. Because that was from an editbox, mIRC would have just echoed "% [ $+ [ test ] ]", letter for letter, if you had used a single slash.

The double slash forces it to evaluate what it is given, resulting in it echoing "hello" as it should do. However, when scripts are in aliases or remotes (where they belong!), this is not needed. The help file says:

"In scripts, all lines are assumed to start with a command, so you don't need to use the / command prefix."

In fact, everything is treated as if it started with a //. Variables and identifiers are (nearly) always evaluated when in a script. Occasionally you need to force mIRC to evaluate them "more" or "again", and that is what the [ ] in the example above are for.

2. The Evaluation Brackets  Back to Top

It's a strange concept, variables about variables, where which variable is used depends on the contents of another variable or identifier. I think of it as a very high level feature faintly similar to pointers, but assuming you have it sorted, it should be obvious how to use the technique to create fake arrays. This alias demonstrates:

testarray { 
  SET %array.1 first 
  SET %array.2 second 
  %choice = $$?="Enter 1 or 2:" 
  echo -s You chose the %array. [ $+ [ %choice ] ] element of the array. 

This technique can be used for many other very useful purposes, for example, to keep information about different nicks in a channel:

SET %swear. [ $+ [ $nick ] ] $1 

This basically sets a variable that is based on the nickname. Here is a simple example that saves the quit message for any nickname that quits, then redisplays it when they join.

on *:QUIT:{ 
  SET %quitmsg. [ $+ [ $nick ] ] $1- 
on *:JOIN:#:{ 
  echo 6 $chan *** $nick last quit- %quitmsg. [ $+ [ $nick ] ] 

This example should help you with creating your own nick-based lookups and all sorts of similar constructs.