Variables
A variable is a temporary storage space, an
"item" that can store a number, a string, a character, or most
anything you set it to. Variables has to be assigned a value to actually return
any value, an unassigned
variable will not have any value, thus it will not exist (there's a few
exceptions to this, which we'll come back
to later on). Being temporary storage holders, variables are useful for storing
data used in commands and
functions such as loops, dialogs (temporary storing input until the
"Ok" button actually is pressed),
awayreasons, temporary data in a calculation, etc. Many people use variables to
store settings for files,
which is a less good idea, both due to the non-permanent nature of variables,
and since many scripts unset
variables, and may unintendedly unset variables used for storing settings.
Setting and unsetting variables
To assign a value to a variable, we use the /set
command. /set %temp testing
will store the string testing
in
the variable %temp,
which then may be used for anything you please...type //echo
-a %temp to see what's
stored in %temp. To remove the assigned value and remove the variable, use the /unset
command. /unset
%temp will clear the
value of %temp, and remove the variable from the variablelist. Both /set and
/unset has
switches which modify the way they perform the action, /set -s
will echo the value you're assigning the
variable to, as you are setting it, whereas /set -uN
will set the variable, and then unset it after N seconds. If
N is 0, the variable will be unset as soon as the current command/function has
finished performing (this is
very useful for loops and functions where the variable only really is needed
while the function is performing.
When unsetting a variable, you may use wildcards to unset more than one variable
at once, as well as
specifying two or more variablenames in a single line..f.ex /unset
%temp* or /unset %temp
%test. A special
variation of the /unset command is the /unsetall,
which unset's all variables from the variable list.
Modifying variables
You may increase the value of a variable by using the /inc
command, if you specify a value that value will
be added to the variable, otherwise the variable's value will be increased by 1.
If the variable did not exist
before you used the /inc command, it will be created with a value equaling the
/inc's value, if no value
specified, it will have the value 1. You may use the -uN
switch with /inc as well, in a similar way as with
/set. Likewise, you may use /dec
to decrease the value of the variable. Similarly; if the variable did not exist
before the /dec, it will be created with a value equaling the /dec's value.
Using variables
So, what do we use variables for? Well, as mentioned
before we may use them for storing data for use in
functions such as loops, awayreasons, temporary data in calculations etc..let's
look at an example: This may
be used in an awayscript, where the we wish to say how long we've been away when
we return. We can
use the identifier
$ctime for this.
$ctime by itself returns the number of seconds since October 28, 1981
(don't ask me why 1981 =P), based on your current system time. This we can use
in a simple calculation
like this..:
Go away:{ set
%atime $ctime | ame is away $$?="Why are you
going away?" | .away $! }
Return:{ ame is back, was away for $duration($calc($ctime
- %atime)) | .away }
That's a basic popup
for going away/returning. The Go away popup assigns the variable %atime
to the
value of $ctime, then sends a message to the channel in the form of an action (/me)
stating that you're away,
and why, then sets you away with that reason. The return popup sets you back,
and announces this to the
channel. $ctime - %atime
finds the number of seconds you've been away, while $duration
converts this
number to weeks/days/hours/minutes/sec's.
other example, we want to list the files
in c:\temp, and echo this into the status window.
alias listfiles {
set -u0 %max $findfile(c:\temp,*.*,0)
echo -s Listing files in c:\temp..
:loop
inc -u0 %c
if (%c <= %max) { echo -s
$findfile(c:\temp,*.*,%c) | goto loop
}
echo -s Finished listing files in c:\temp!
}
Here the variables %max
and %c control the
loop, what this alias does is that it first assigns %max to the
number of files in c:\temp, then it sets up a loop that lasts as long as %c is
smaller or equal to %max, thus
listing file 1 through %max in the loop. By using the -u0 switch with the inc
and set command, we ensure
that the variables are gone as soon as the loop is done with, by far the
cleanest way of making a script.
Local variables
With mIRC 5.6 came a new kind of variables, local variables. These variables are
"local" to a scriptline
and are unset once the script finishes performing, and can only be used by the
script itself (thus not called
from the commandline), much similar to local
aliases. A local variable is assigned a
value with the /var
command, and can be modified using /inc and /dec as usual. To assign a local
variable, use /var
%variablename = value.