From WikiChip
Difference between revisions of "mirc/commands/while"
(→Example) |
(→Example) |
||
Line 16: | Line 16: | ||
== Example == | == Example == | ||
+ | Basic construct | ||
+ | <syntaxhighlight lang="mIRC"> | ||
+ | ;Create a counter variable, set its value to 1 | ||
+ | var %i 1 | ||
+ | |||
+ | ;Create a stop variable with a value equal to the number at which you want to stop the loop. | ||
+ | var %stop 5 | ||
+ | |||
+ | ;Start of loop with a condition in () - work while the number in the variable %i is less than or equal to the number in the variable %stop | ||
+ | while (%i <= %stop) { | ||
+ | |||
+ | ;Print in echo the value of the counter variable | ||
+ | echo -a Number in counter: %i | ||
+ | |||
+ | ;Increase the counter variable by +1 | ||
+ | inc %i | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
How to iterate over lines of a file | How to iterate over lines of a file | ||
<syntaxhighlight lang="mIRC"> | <syntaxhighlight lang="mIRC"> |
Revision as of 02:57, 1 September 2025
Commands & Identifiers
Basics
Events
Matching Tools
Data Storage
Control Structures
GUI Scripting
Sockets
Advanced Scripting
Additional Resources
Security
Other
The /while command is a construct in the mIRC scripting language that can perform repetitive operations. The block of statement inside the while loop will get executed as long as the condition is true. You can manually break out of a loop by using the Cltr+Break key combinations.
Synopsis
while (condition) { /statements /statements /statements }
Switches
None
Parameters
None
Example
Basic construct
;Create a counter variable, set its value to 1 var %i 1 ;Create a stop variable with a value equal to the number at which you want to stop the loop. var %stop 5 ;Start of loop with a condition in () - work while the number in the variable %i is less than or equal to the number in the variable %stop while (%i <= %stop) { ;Print in echo the value of the counter variable echo -a Number in counter: %i ;Increase the counter variable by +1 inc %i }
How to iterate over lines of a file
;Read all lines of a file: var %i 1 | while (%i <= $lines(file.txt)) { echo -a line %i - $read(file.txt,nt,%i) inc %i } ;Read from line 5 and stop at line 10: var %i 5 | while (%i <= $lines(file.txt)) { echo -a line %i - $read(file.txt,nt,%i) if (%i == 10) break inc %i } ;Note: These are the code snippets that you should use inside your alias or script. ;Example with an alias: alias whiletest { var %i 1 | while (%i <= $lines(file.txt)) { echo -a line %i - $read(file.txt,nt,%i) inc %i } } ;To call the alias use the command: /whiletest
A simple counter to count the number of loop iterations
alias CountToTen { ;Create a counter variable, set it to one var %counter 1 ;Loop while the counter variable is less than or equal to ten. while (%counter <= 10) { ;Print out the value of the counter variable echo -a Count Number: %counter ;Increase the counter variable by 1 inc %counter } } ;Call the alias with the command: /CountToTen
The above example will output:
Count Number: 1 Count Number: 2 Count Number: 3 Count Number: 4 Count Number: 5 Count Number: 6 Count Number: 7 Count Number: 8 Count Number: 9 Count Number: 10
Print a list all variable names with their values
alias ListVars { ;Check if there are any variables set if (!$var(*,0)) { echo -a There Are No Variables. ;Return command to terminate alias function return } ;Set a counter variable var %x 1 ;Loop while the counter variable is less than or equal to the total number of variables while (%x <= $var(*,0)) { ;Print out the variable and its value echo -a %x $+ ) $var(*,%x) = $var(*,%x).value ;Increase the variable by one inc %x } } ;Call the alias with the command: /ListVars
The above example will output something like this:
1) %Var = Value 2) %Foo = FooBar 3) %Bar = BarFoo
Compatibility
Added: mIRC v5.7
Added on: 02 Feb 2000
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.
See also
mIRC commands list