Difference between revisions of "While loops"
m (mostly modified links) |
m (added an example loop for listing stuff into a var) |
||
Line 73: | Line 73: | ||
inc %i | inc %i | ||
} | } | ||
+ | |||
+ | |||
+ | Listing something into a variable with loop: | ||
+ | |||
+ | If you wish to loop through something (let's say you want to list all the loaded scriptfiles), and echo them in one line, you could do:<br /> | ||
+ | '''Note''' also that variables in mirc can only hold ~950 characters, depending on the variable name | ||
+ | var %x = 1 | ||
+ | while (%x <= $script(0)) { | ||
+ | ; put the earlier content of %y and the new scriptfile into the variable %y | ||
+ | var %y = %y $script(%x) | ||
+ | inc %x | ||
+ | } | ||
+ | ; echo the variable where we listed those scripts | ||
+ | echo -ag %y | ||
+ | |||
== See Also == | == See Also == | ||
* [[If-Then-Else]] | * [[If-Then-Else]] | ||
[[Category:Commands]] | [[Category:Commands]] |
Revision as of 21:01, 28 January 2007
While loops
While loops allow you to repeat a set of commands while the expression inside the brackets is $true.
while (expression) { commands }
The expression is evaluated in the same manner as If-Then-Else expressions are evaluted and it uses the same operators. If the expression is $true the commands inside the brackets are run while the expression remains $true.
You can break out of the while loop by issuing the break command, or jump to the start of the while loop by issuing the continue command.
Every time the while loop jumps back to the top, the expression is re-evaluated.
Note: If the expression evaluation changes to $false during the while loop you will not break out of the while loop until the while loop has reached the end of its contents.
Examples
Example shows how to get a list of users in the channel:
var %i = $nick(#,0) ;Sets %i to the amount of users in the channel. while (%i) { var %nick = $nick(#,%i) ;Find the nth user in the channel. var %people = $addtok(%people,%nick,44) ;Add %nick to %people using $chr(44) (a comma) as the delimiter. dec %i } echo -ag Current channel users: %people
Example shows how to loop through all channels and echo the number of people in these channels.
var %i = 1 ; loop from 1 to total number of channels while (%i <= $chan(0)) { ; echo it echo -ag Channel: $chan(%i) Nicks: $nick($chan(%i),0) ; increase looping variable inc %i }
Example shows how to loop though from 1 to 50:
var %from = 1 var %to = 50 while (%from <= %to) { echo -ag I know I can count to %from inc %from }
Example shows how to use the break command:
var %i = 1 while (%i) { echo -ag $chr(37) $+ i = %i if (%i == 10) { break } ;If %i has a value of 10 then exit the while loop, missing out any commands still left to be executed inside the while loop. inc %i }
Example shows how to use the continue command. It will echo everything from 1 to 10 except 5, as it jumps to the begin of the loop before coming to the echo command.
var %i = 0 while (%i < 10) { inc %i if (%i == 5) { continue } ;If %i has a value of 5 then jump to the start of the while loop, missing out the echo below. echo -ag $chr(37) $+ i == %i }
Example shows how to loop through a text file. Let's assume we have a "foo.txt" looking like
This is line1 This is line2 This is line3
To loop through it, you'd have to use:
var %i = 1 ; $lines returns the total number of lines in a file while (%i <= $lines(foo.txt)) { echo -a $read(foo.txt,%i) inc %i }
Listing something into a variable with loop:
If you wish to loop through something (let's say you want to list all the loaded scriptfiles), and echo them in one line, you could do:
Note also that variables in mirc can only hold ~950 characters, depending on the variable name
var %x = 1 while (%x <= $script(0)) { ; put the earlier content of %y and the new scriptfile into the variable %y var %y = %y $script(%x) inc %x } ; echo the variable where we listed those scripts echo -ag %y