10.2 Looping for every value
An other loop is the foreach loop.
In this loop you put one or more lists and than a set of commands will be executed for every object in that list.
The syntax of a foreach command is foreach <string> <list> [<second string> <second list> ...] { <body> }.
The <string>'s are in which string the object must be placed.
You can put as many lists into the loop as you like, just as long as there is always a string with it to put
the object in. Also because foreach already knows that its dealing with a string here you musn't put a $ in
front of it.
The <string> will contain the object of the list as if [lindex] was used on it.
The <list> is the list of which the loop must get the objects from.
The <body> is the set of commands you want to execute and works in the same way as the <body> of the
while loop.
Here is an example of a foreach loop:
set ops 0
foreach nickname [chanlist #eggdrop] {
if {[isop $nickname #eggdrop]} {
incr ops
}
}
puthelp "PRIVMSG #eggdrop :There are currently $ops people opped in #eggdrop."
|