Difference between revisions of "While loops"
(ehm, $nick(#,0) returns total number of people in the channel, not $chan(#,0)) |
() too much) |
||
Line 19: | Line 19: | ||
while (%i) { | while (%i) { | ||
var %nick = $nick(#,%i) ;Find the ''n<sup>th</sup>'' user in the channel. | var %nick = $nick(#,%i) ;Find the ''n<sup>th</sup>'' user in the channel. | ||
− | var %people = [[$addtok]](%people,%nick | + | var %people = [[$addtok]](%people,%nick,44) ;Add ''%nick'' to ''%people'' using [[$chr]](44) (a comma) as the delimiter. |
[[dec]] %i | [[dec]] %i | ||
} | } |
Revision as of 21:07, 22 December 2005
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. 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 revaluated.
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 completed.
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 }