From WikiChip
Difference between revisions of "mirc/commands/while"
< mirc‎ | commands

(Example)
(Synopsis)
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{mirc title|/while Command}}
 
{{mirc title|/while Command}}
The '''/while command''' is a [[While Loops - mIRC|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.
+
The '''/while command''' is a [[While Loops - mIRC|construct in the mIRC scripting language]] that can perform repetitive operations. The block of statements and commands inside the body of the while loop will be executed as long as the condition is true. You can manually break out of a loop by using the Cltr+Break key combinations.
  
 
== Synopsis ==
 
== Synopsis ==
 
  while (condition) {
 
  while (condition) {
   /statements
+
    
   /statements
+
   looping repeating operations and commands are located here
   /statements
+
    
 +
  command1 ...
 +
  command2 ...
 +
  command3 ...
 +
  ...
 
  }
 
  }
  
Line 16: Line 20:
  
 
== Example ==
 
== Example ==
How to iterate over lines of a file
+
Basic construct
 
<syntaxhighlight lang="mIRC">
 
<syntaxhighlight lang="mIRC">
;Read all lines of a file:
+
;Create a counter variable, set its value to 1
var %i 1 | while (%i <= $lines(file.txt)) {
+
var %i 1
  echo -a line %i - $read(file.txt,nt,%i)
+
 
   inc %i
+
;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 round brackets (%i <= %stop) - work while the number in the variable %i is < less than or = equal to the number in the variable %stop
 +
while (%i <= %stop) {
 +
 
 +
  ;Print echo message to active window with value of counter variable
 +
   echo -a Number in counter: %i
  
;Read from line 5 and stop at line 10:
+
  ;Increase the counter variable by +1
var %i 5 | while (%i <= $lines(file.txt)) {
 
  echo -a line %i - $read(file.txt,nt,%i)
 
  if (%i == 10) break
 
 
   inc %i
 
   inc %i
 
}
 
}
 +
</syntaxhighlight>
 +
'''Note:''' Use this or a similar construct/snippet of code inside any of your aliases or scripts as shown in the examples to create a loop.
  
;Note: These are the code snippets that you should use inside your alias or script.
 
  
 
+
Example of a simple counter to count the number of iterations of a loop
;Example with an alias:
+
<syntaxhighlight lang="mIRC">
alias whiletest {
+
;Count the number of iterations from 1 to 5
   var %i 1 | while (%i <= $lines(file.txt)) {
+
alias whilecounter {
     echo -a line %i - $read(file.txt,nt,%i)
+
   var %i 1, %stop 5
 +
  while (%i <= %stop) {
 +
     echo -a Number in counter: %i
 
     inc %i
 
     inc %i
 
   }
 
   }
 
}
 
}
  
;To call the alias use the command: /whiletest
+
;To call the alias enter the command: /whilecounter
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
The above example will output:
 +
<pre>Number in counter: 1
 +
Number in counter: 2
 +
Number in counter: 3
 +
Number in counter: 4
 +
Number in counter: 5</pre>
  
Other examples of using a loop
 
  
Example 1:
+
Example of reading a file by iterating over each line
 
<syntaxhighlight lang="mIRC">
 
<syntaxhighlight lang="mIRC">
;Example 1
+
;Read all lines of a file
 +
alias whilereadfile {
 +
  var %i 1 | while (%i <= $lines(file.txt)) {
 +
    echo -a line %i - $read(file.txt,nt,%i)
 +
    inc %i
 +
  }
 +
}
  
alias CountToTen {
+
;To call the alias enter the command: /whilereadfile
  ;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
+
;Read from line 5 and stop at line 10
     echo -a Count Number: %counter
+
alias whilereadfile2 {
 
+
  var %i 5 | while (%i <= $lines(file.txt)) {
     ;Increase the counter variable by 1
+
     echo -a line %i - $read(file.txt,nt,%i)
     inc %counter
+
     if (%i == 10) break
 +
     inc %i
 
   }
 
   }
 
}
 
}
  
;Call the alias with the command: /CountToTen
+
;To call the alias enter the command: /whilereadfile2
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The above example will output:
 
 
<pre>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</pre>
 
 
Example 2:
 
  
 +
Example of how to print a list all variable names with their values
 
<syntaxhighlight lang="mIRC">
 
<syntaxhighlight lang="mIRC">
 
+
alias whilelistvars {
;Example 2
 
 
 
alias ListVars {
 
 
   ;Check if there are any variables set
 
   ;Check if there are any variables set
 
   if (!$var(*,0)) {
 
   if (!$var(*,0)) {
     echo -a There Are No Variables.
+
     echo -a There are no variables.
     halt
+
     ;Return command to terminate alias function
 +
    return
 
   }  
 
   }  
  
 
   ;Set a counter variable
 
   ;Set a counter variable
   var %a 1
+
   var %x 1
  
 
   ;Loop while the counter variable is less than or equal to the total number of variables
 
   ;Loop while the counter variable is less than or equal to the total number of variables
   while (%a <= $var(*,0)) {  
+
   while (%x <= $var(*,0)) {  
  
 
     ;Print out the variable and its value
 
     ;Print out the variable and its value
     echo -a $v1 $+ ) $var(*,$v1) = $var(*,$v1).value  
+
     echo -a %x $+ ) $var(*,%x) = $var(*,%x).value  
  
 
     ;Increase the variable by one
 
     ;Increase the variable by one
     inc %a
+
     inc %x
 
   }
 
   }
}</syntaxhighlight>
+
}
 +
 
 +
;To call the alias enter the command: /whilelistvars
 +
</syntaxhighlight>
  
 
The above example will output something like this:
 
The above example will output something like this:
  
<pre>1) %Foo = FooBar
+
<pre>1) %Var = Value
2) %Bar = BarFoo</pre>
+
2) %Foo = FooBar
 +
3) %Bar = BarFoo</pre>
 +
 
 +
 
 +
Example of how to print a list all open windows with their names
 +
<syntaxhighlight lang="mIRC">
 +
;Count the number of all open windows
 +
alias whilewindows {
 +
  echo -a Channel windows: $+([,$chan(0),])
 +
  var %c 1 | while (%c <= $chan(0)) { echo -a %c : $chan(%c) | inc %c }
 +
  echo -a $str(-,30)
 +
 
 +
  echo -a Private windows: $+([,$query(0),])
 +
  var %q 1 | while (%q <= $query(0)) { echo -a %q : $query(%q) | inc %q }
 +
  echo -a $str(-,30)
 +
 
 +
  echo -a Custom windows: $+([,$window(0),])
 +
  var %w 1 | while (%w <= $window(0)) { echo -a %w : $window(%w) | inc %w }
 +
  echo -a $str(-,30)
 +
 
 +
  echo -a Dialog windows: $+([,$dialog(0),])
 +
  var %d 1 | while (%d <= $dialog(0)) { echo -a %d : $dialog(%d) | inc %d }
 +
  echo -a $str(-,30)
 +
}
 +
 
 +
;To call the alias enter the command: /whilewindows
 +
</syntaxhighlight>
  
 
== Compatibility ==
 
== Compatibility ==

Latest revision as of 09:23, 9 September 2025

The /while command is a construct in the mIRC scripting language that can perform repetitive operations. The block of statements and commands inside the body of the while loop will be executed as long as the condition is true. You can manually break out of a loop by using the Cltr+Break key combinations.

Synopsis[edit]

while (condition) {
  
  looping repeating operations and commands are located here
  
  command1 ...
  command2 ...
  command3 ...
  ...
}

Switches[edit]

None

Parameters[edit]

None

Example[edit]

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 round brackets (%i <= %stop) - work while the number in the variable %i is < less than or = equal to the number in the variable %stop
while (%i <= %stop) {
 
  ;Print echo message to active window with value of counter variable
  echo -a Number in counter: %i
 
  ;Increase the counter variable by +1
  inc %i
}

Note: Use this or a similar construct/snippet of code inside any of your aliases or scripts as shown in the examples to create a loop.


Example of a simple counter to count the number of iterations of a loop

;Count the number of iterations from 1 to 5
alias whilecounter {
  var %i 1, %stop 5
  while (%i <= %stop) {
    echo -a Number in counter: %i
    inc %i
  }
}
 
;To call the alias enter the command: /whilecounter

The above example will output:

Number in counter: 1
Number in counter: 2
Number in counter: 3
Number in counter: 4
Number in counter: 5


Example of reading a file by iterating over each line

;Read all lines of a file
alias whilereadfile {
  var %i 1 | while (%i <= $lines(file.txt)) {
    echo -a line %i - $read(file.txt,nt,%i)
    inc %i
  }
}
 
;To call the alias enter the command: /whilereadfile
 
 
;Read from line 5 and stop at line 10
alias whilereadfile2 {
  var %i 5 | while (%i <= $lines(file.txt)) {
    echo -a line %i - $read(file.txt,nt,%i)
    if (%i == 10) break
    inc %i
  }
}
 
;To call the alias enter the command: /whilereadfile2


Example of how to print a list all variable names with their values

alias whilelistvars {
  ;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
  }
}
 
;To call the alias enter the command: /whilelistvars

The above example will output something like this:

1) %Var = Value
2) %Foo = FooBar
3) %Bar = BarFoo


Example of how to print a list all open windows with their names

;Count the number of all open windows
alias whilewindows {
  echo -a Channel windows: $+([,$chan(0),])
  var %c 1 | while (%c <= $chan(0)) { echo -a %c : $chan(%c) | inc %c }
  echo -a $str(-,30)
 
  echo -a Private windows: $+([,$query(0),])
  var %q 1 | while (%q <= $query(0)) { echo -a %q : $query(%q) | inc %q }
  echo -a $str(-,30)
 
  echo -a Custom windows: $+([,$window(0),])
  var %w 1 | while (%w <= $window(0)) { echo -a %w : $window(%w) | inc %w }
  echo -a $str(-,30)
  
  echo -a Dialog windows: $+([,$dialog(0),])
  var %d 1 | while (%d <= $dialog(0)) { echo -a %d : $dialog(%d) | inc %d }
  echo -a $str(-,30)
}
 
;To call the alias enter the command: /whilewindows

Compatibility[edit]

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[edit]

v · d · e mIRC commands list

A /abook, /action, /add, /ajinvite, /alias, /aline, /ame, /amsg, /anick, /aop, /auser, /auto, /autojoin, /avoice, /away

B /background, /ban, /bcopy, /beep, /bindip, /bread, /break, /breplace, /bset, /btrunc, /bunset, /bwrite

C /channel, /clear, /clearall, /clearial, /cline, /clipboard, /close, /closechats, /closedccs, /closefserves, /closemsg, /cnick, /color, /colour, /comclose, /comlist, /commands, /comopen, /comreg, /continue, /copy, /creq, /ctcp, /ctcpreply, /ctcps

D /dcc, /dccserver, /dde, /ddeserver, /debug, /dec, /describe, /dialog, /did, /didtok, /disable, /disconnect, /dlevel, /dline, /dll, /donotdisturb, /dns, /dqwindow, /drawcopy, /drawdot, /drawfill, /drawline, /drawpic, /drawrect, /drawreplace, /drawrot, /drawsave, /drawscroll, /drawsize, /drawtext

E /ebeeps, /echo, /editbox, /else, /elseif, /emailaddr, /enable, /events, /exit

F /fclose, /filter, /findtext, /finger, /firewall, /flash, /flist, /flood, /flush, /flushini, /fnord, /font, /fopen, /fseek, /fsend, /fserve, /fullname, /fupdate, /fwrite

G /ghide, /gload, /gmove, /gopts, /goto, /gplay, /gpoint, /gqreq, /groups, /gshow, /gsize, /gstop, /gtalk, /gunload, /guser

H /hadd, /halt, /haltdef, /hdec, /hdel, /help, /hfree, /hinc, /hload, /hmake, /hotlink, /hop, /hsave

I /ial, /ialclear, /ialmark, /identd, /if, /ignore, /iline, /inc, /iuser

J /join

L /leave, /linesep, /links, /list, /load, /loadbuf, /localinfo, /log, /logview

M /maxdepth, /mdi, /me, /menubar, /mkdir, /mnick, /mode, /msg

N /noop, /notice, /notify

O /onotice, /omsg

Q /qme, /qmsg, /query, /queryrn, /quit, /quote

P /parseline, /part, /partall, /pdcc, /perform, /play, /playctrl, /pop, /protect, /proxy, /pvoice

R /raw, /registration, /reload, /remini, /remote, /remove, /rename, /renwin, /reseterror, /resetidle, /return, /returnex, /rlevel, /rline, /rmdir, /run, /ruser

S /save, /savebuf, /saveini, /say, /scid, /scon, /server, /set, /setlayer, /showmirc, /signal, /sline, /sockaccept, /sockclose, /socklist, /socklisten, /sockmark, /sockopen, /sockpause, /sockread, /sockrename, /sockudp, /sockwrite, /sound, /speak, /splay, /sreq, /strip, /switchbar

T /timer, /timestamp, /tip, /tips, /titlebar, /tnick, /tokenize, /toolbar, /topic, /tray, /treebar

U /ulist, /unload, /unset, /unsetall, /updatenl, /url, /username, /uwho

V /var, /vcadd, /vcmd, /vcrem, /vol

W /wallchops, /wavplay, /while, /window, /winhelp, /write, /writeini

X /xyzzy