Guide to TCL scripting for Eggdrop 1.6

[ Previous ] [ Index ] [ Next ]
[ Text version ]


10. Looping

In this chapter you will learn about different kinds of loops.
Under many circumstances it is very handy if you can make your script perform the same set of commands multiple times, because often you have no control over how big something is and thus how often the same set of commands must be performed.

10.1 Looping under conditions

The most basic loop is the while loop.
This will keep going until a set of given actions match each other. The while command also looks a lot like the if command.
The syntax of a while command is while { <check> } { <body> }.

The <check> works in exactly the same way as the if command.
It's possible to simply replace "while" with "if" and make the script execute the set of commands only once without running into any errors except for ones that come from your script, but not from the while command itself.

The <body> is also just like in the if command and is the set of commands you want the loop to execute while it's running.

Here is an example of a while loop:

set test "[chanlist #eggdrop]"
set ops 0
while {$test != ""} {
  if {[isop [lindex $test 0] #eggdrop]} {
    incr ops
  }
  set test "[lreplace $test 0 0]"
}
puthelp "PRIVMSG #eggdrop :There are currently $ops people opped in #eggdrop."

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."

10.3 Looping for a number of times

The third and final loop is the for loop.
This loop is fairly simple and is mostly used when somebody wants to loop something for a number of times.
The syntax of a for command is for { <start> } { <check> } { <loop> } { <body> }.

The <start> is the command you want to be executed before the loop starts.
This can be setting a string to 0 for instance.

The <check> is the check that is made while the loop is running and works in the same way as the check in the while loop.

The <loop> is the command you want to be executed every time a loop has ended.
This can be increasing a string with 1 for instance.

Here is an example of a for loop:

set test "[chanlist #eggdrop]"
set ops 0
for { set number 0 } { $number < [llength $test] } { incr number } {
  if {[isop [lindex $test $number] #eggdrop]} {
    incr ops
  }
}
puthelp "PRIVMSG #eggdrop :There are currently $ops people opped in #eggdrop."

10.4 Writing your own script

Lets start a new script. Make a script that when a global operator messages "ops" to the bot, it sends a message to all the channels how many people there are currently opped there and how many bot operators there are in it.
Remember that the goal is not to make the best and most efficient script right now, but trying to put into it as many things as possible that you've just learned.
You can find an example script here: example-ch10.tcl.


[ Text version ]
[ Previous ] [ Index ] [ Next ]


Design & Graphics by Shawn Borton of CalcioStar.com