From WikiChip
Editing mirc/commands/var

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{mirc title|/var Command}}
 
 
The '''/var command''' can be used to replace the value of existing variables or create a new variables and assign them values. By default /var creates local variable, this can be changed via the -g switch ({{mIRC|/set}} command can be used to set global variables by default).
 
The '''/var command''' can be used to replace the value of existing variables or create a new variables and assign them values. By default /var creates local variable, this can be changed via the -g switch ({{mIRC|/set}} command can be used to set global variables by default).
  
The Local Variables created by /var exist only within the :event: or ALIAS where they were created. If your event or alias calls another alias, that alias cannot see the local values unless passed to them in another way, such as a parameter used when calling the alias.
+
The var command can perform one math operation via one of the arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). They ''must'' be space delimited with both operands being a number. If any of the operands are not a number or if a space is missing, it will be treated as plain text.
  
The var command can perform one math operation via one of the arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), modulo (%), bitwise-and (&), and exponent (^). They ''must'' be space delimited with both operands being a number. If any of the operands are not a number or if a space is missing, it will be treated as plain text.
 
  
 
Note: Unlike the {{mIRC|/set}} command, var can assign a value to multiple variables at once.
 
Note: Unlike the {{mIRC|/set}} command, var can assign a value to multiple variables at once.
Line 11: Line 9:
 
Internally, the var command simply breaks down the line (by commas) and passes the arguments to /set. The set command has its own evaluation routine which lets you dynamically concatenate additional values onto the variable name before the assignment takes place.
 
Internally, the var command simply breaks down the line (by commas) and passes the arguments to /set. The set command has its own evaluation routine which lets you dynamically concatenate additional values onto the variable name before the assignment takes place.
  
<syntaxhighlight lang="mIRC">//var -s % $+ $ctime ABC</syntaxhighlight>
+
<syntaxhighlight lang="mIRC">//var -ls % $+ $ctime ABC</syntaxhighlight>
  
 
Will create a variable like %1209425041 with the value of 'ABC'; Similarly this can be used with $nick and $chan in on events.
 
Will create a variable like %1209425041 with the value of 'ABC'; Similarly this can be used with $nick and $chan in on events.
  
 
== Synopsis ==
 
== Synopsis ==
  /var [-sgnip] <%var> [[= ]value]
+
  /var [-sg] <%var> [[= ]value]
 
  /var [-sg] <%var> <number> <+ | - | * | / | %> <number>
 
  /var [-sg] <%var> <number> <+ | - | * | / | %> <number>
  /var [-sg] <%var> [= ]<number> <+ | - | * | / | % | &> <number>
+
  /var [-sg] <%var> [= ]<number> <+ | - | * | / | %> <number>
 
  /var [-sg] <%var> [[= ]value][, <%var> [[= ]value]][, ...]
 
  /var [-sg] <%var> [[= ]value][, <%var> [[= ]value]][, ...]
  /var [-sg] <%var> [= ]<number> <+ | - | * | / | % | &> <number>[, ...]
+
  /var [-sg] <%var> [= ]<number> <+ | - | * | / | %> <number>[, ...]
  
 
== Switches ==
 
== Switches ==
 
* '''-s''' - Display variable assignment value
 
* '''-s''' - Display variable assignment value
 
* '''-g''' - Creates a global variable instead
 
* '''-g''' - Creates a global variable instead
* '''-n''' - Prevents math operation
+
* '''-n''' - Treats value as plain text
* '''-i''' - Creates the local variable only if that variable does not exist AS A LOCAL VARIABLE, even if $null
 
* '''-p''' - Permits value to be 2 literal doublequotes and permits value to end with a single $chr(32) space. Also performs -n switch behavior
 
<br />
 
* Note: If you are placing an event's $1- into a variable, you should use -p to prevent 1 + 1 being evaluated to 2, or "" not being set
 
* Note: The = in "var %a = value" is optional and is not placed into the value.
 
* Note: See the {{mIRC|/set}} page for more info about setting dynamic variable names using identifiers like $nick or $network
 
* Note: much of the usage of /var is similar to that of /set except that switches like -uN or -e or -z or -k  have no meaning for local variables which cease to exist when the event or alias is finished.
 
  
 
== Parameters ==
 
== Parameters ==
 
* '''<%var>''' - The name of the variable
 
* '''<%var>''' - The name of the variable
* '''[value]''' - The value to assign to the variable. If not present or is $null the local var is unset
+
* '''[value]''' - The value to assign to the variable
 
* '''<number>''' - Any arbitrary numerical value, can be a floating point number
 
* '''<number>''' - Any arbitrary numerical value, can be a floating point number
* '''< + | - | * | / | % | ^ | & >''' - One of the seven possible arithmetic operations
+
* '''<+ |''' - | * | / | %> - One of the five possible arithmetic operations
 
* '''<number>''' - Any arbitrary numerical value, can be a floating point number
 
* '''<number>''' - Any arbitrary numerical value, can be a floating point number
 
'''Note''': if your [value] ends with a literal space seperated comma, mIRC will see it as a new variable assignement even if no '%' is found, you need to use two commas to produce one in this case: //var %a test ,,
 
  
 
== Example ==
 
== Example ==
Line 55: Line 44:
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
<syntaxhighlight lang="mIRC">
+
== Compatibility ==
-s can be used to debug scripts, showing variables being set to their values. The message displays to the active window if typed in editbox, or Status Window if in an alias/event:
+
Added: mIRC v5.6
//var -s %a test
 
returns: * Set %a to test</syntaxhighlight>
 
 
 
<syntaxhighlight lang="mIRC">
 
-g sets global as if you used /set instead of /var:
 
//unset %a %a | var -g %a global | echo -a $var(%a,1).local $var(%a,1).value | .timer 1 0 echo 5 -a timer sees % $+ a
 
returns:
 
$false global
 
timer sees global</syntaxhighlight>
 
 
 
<syntaxhighlight lang="mIRC">
 
var defaults to using math operations if there are 3 tokens separated by spaces, and the 1st and 3rd are numbers, and the 2nd token is any of the 6 operators listed above:
 
//var %a 1 + 2 | echo -a %a
 
returns: 3
 
//var %a $pi - 1 | echo -a %a
 
returns: 2.141593
 
//tokenize 32 4 5 | var %a $1 * $2 | echo -a %a
 
returns: 20
 
//var %b 7 | var %a %b / 3 | echo -a %a
 
returns: 2.333333
 
//var %a $ctime % 3600 | echo -a %a seconds since the top of the hour
 
returns: <number> seconds since the top of the hour
 
//var %a 2^16 | var %b 2 ^ 16 | echo -a %a because not tokenized by spaces vs %b
 
returns: 2^16 because not tokenized by spaces vs 65536
 
//var %b 7 , %c 11 , %a %b & %c | echo -a a= %a b= $base(%b,10,2) c= $base(%c,10,2) b&c= $and(%b,%c)
 
returns: 3 because bit-1 and bit-2 are the only common bits between 7 and 11
 
//var %a 1 + 1 + 1 | echo -a %a because only 1 math operation allowed
 
returns: 1 + 1 + 1 because only 1 math operation allowed
 
Prevent math operation with -n switch
 
//var %a 1 + 1 | var -n %b 1 + 1 | echo -a %a vs %b
 
returns: 2 vs 1 + 1</syntaxhighlight>
 
 
 
<syntaxhighlight lang="mIRC">
 
-i causes /var to act only if local var does not exist, even if it is $null. "/var %a" does not unset the local var, it sets it to $null:
 
//set %a Global | var -i %a Local1 | echo -a Value1: %a | var -i %a Local2 | echo -a Value2: %a | var %a | echo -a Value3: %a | var -i %a Local3 | echo -a Value4: %a | echo -a $var(%a,1).local / $var(%a,1).value vs $var(%a,2).local / $var(%a,2).value
 
returns:
 
Value1: Global
 
Value2: Local2
 
Value3:
 
Value4:</syntaxhighlight>
 
 
 
<syntaxhighlight lang="mIRC">
 
By default, /var and /set do not allow value to be a pair of double quotes nor to end with a single space (multiple spaces can be set)
 
The -p changes /var and /set to allow these values:
 
//var -p %a "" | var -p %b test $+ $chr(32)  | echo -a %a vs $len(%b)
 
returns: "" vs 5
 
//var    %a "" | var    %b test $+ $chr(32)  | echo -a %a vs $len(%b)
 
returns: vs 4
 
-p also includes -n blocking of math operation:
 
//var -p %a 1 + 1 | echo -a %a
 
returns: 1 + 1</syntaxhighlight>
 
  
<syntaxhighlight lang="mIRC">
 
The = is no longer required, and is a difference in behavior between /set and /var, though it makes it easier to make a local var beginning with the = symbol:
 
//set %a = testa | set %b = = testb | echo -a 1. %a vs %b
 
//var %a = testa | var %b = = testb | echo -a 2. %a vs %b
 
Return:
 
1. = testa vs = = testb
 
2. testa vs = testb</syntaxhighlight>
 
 
By default, /var and /set do not allow value to be a pair of double quotes nor to end with a single space (multiple spaces can be set)
 
<syntaxhighlight lang="mIRC">
 
The -p changes /var and /set to allow these values:
 
//var -p %a "" | var -p %b test $+ $chr(32)  | echo -a 1. %a vs $len(%b)
 
//var    %a "" | var    %b test $+ $chr(32)  | echo -a 2. %a vs $len(%b)
 
Return:
 
1. "" vs 5
 
2. vs 4</syntaxhighlight>
 
 
== Compatibility ==
 
{{mIRC compatibility|5.6}}
 
 
== See also ==
 
== See also ==
 +
* [[List of commands - mIRC|List of commands]]
 +
* [[List of identifiers - mIRC|List of identifiers]]
 
* {{mIRC|$var}}
 
* {{mIRC|$var}}
 
* {{mIRC|/set}}
 
* {{mIRC|/set}}
 
* {{mIRC|/unset}}
 
* {{mIRC|/unset}}
 
* {{mIRC|/unsetall}}
 
* {{mIRC|/unsetall}}
* {{mIRC|$calc}}
+
{{mIRC command list}}
 +
 
 +
[[Category:MIRC commands]]

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)