Guide to TCL scripting for Eggdrop 1.6

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


5. Variables

You've already come across the term variable a few times in this Guide, but in this chapter you'll learn what they exactly are.

5.1 What variables are

Variables are used in almost every TCL script. I haven't seen a TCL script yet that didn't use them. A variable is simply said something in which you can store information.
This information can be anything and is dynamic. It can be different each time you run your procedure or script.

In many programming languages there are different types of variables. You would have to declare each variable and also if it is a number or if it contains letters.
This doesn't apply for TCL. In TCL every variable is a so called string, meaning that it doesn't matter if it contains letter or numbers, it can be one of those or both.

This limits the things you can do with it a bit but it also makes the language a whole lot easier.
From now on I'll also refer to a variable as a string.

5.2 Setting strings

Strings can be set and unset with the set and unset commands.
The syntax of a set command is set <string> <value> and
of the unset command it's unset <string>.
Besides set and unset there is also append.
This command works in the same way as the set command, but the difference is that this command adds something to the string instead of changing it.
The append command adds something directly at the end of a string and is equal to set <string> "$<string><value>".

The <string> is the name of the string you want to change.
The name must consist of letters, marks (-) and/or numbers only. If you put any other characters in it it might cause errors because the script inteprets them wrong.

The <value> is what you want to set the string to or append to the string if you are using append.
This can be a number, some text, the output of a command or anything else for that matter.
What you want to put in your string is not limited to anything, but some characters do need to be escaped by a backslash for instance to make sure TCL doesn't interpret them wrong.
Also note that if you want to put a piece of text in a string that you enclose it in quotes.

Besides setting a string, you can also unset a string. This simply makes the string non-existend again as it was before you created it.

NOTE: TCL will return an error when you try use a string that doesn't exist (a string can exist with nothing in it (set test "") though).

5.3 Using arrays

Besides normal strings, there are also arrays.
An array is a group of strings brought together "under one roof". You can use these arrays in exactly the same way as you can use normal strings and more.
The biggest advantage of an array over a normal string is that because they are all under the same roof they can also be processed all at once with some commands, making it easier for you because you don't have to check all your strings one by one since the entire array is already loaded.

The syntax of an array is $array(string), where array is the name of the array and string is the name of the string within the array.
Once you have created an array you can't create a string with the same name.
For example, if you have $test(<string>), you won't be able to have $test aswell.

Here are a few examples:

#Put the word "that" in the string what of the array test
set test(what) "that"

#Put the word "now" in the string when of the array test
set test(when) "now"

#The following will produce an error since there already is an array called test!
set test "testing"

#Put the word "what" in the string test_what
set test_what "what"

#Put the word "now" in the string test_when
set test_when "now"

5.4 Retrieving an array list

After you've made an array you might want to get a list of all the strings that are in the array, for example getting a list of the array $test() from the previous example.

The command used for this is array names.
The syntax of an array names command is array names <array>.
In this case <array> is the name of the array you want to get the names from like "test" in the example.

Now if we were to put array names test in the previous example, it would result in getting "what" and "when" returned.

Right now you proberbly don't have any use for this, but later on it might come in usefull.

5.5 Local and global strings

There are two types of strings. Local ones and global ones.
A local string only exists while a procedure is running and a global string exists as long as your script is running so in this case while your bot is running.
All the strings created outside a procedure are automaticly global strings and all the strings created within a procedure are automaticly local strings.
While you are in a procedure you can turn a local string into a global string by using the global command.
Global and local strings work in exactly the same way, the only difference is when and where they exist.

The syntax of a global command is global <strings>.
Every string must be seperated by a space and if you want to have a global array, you only have to give the array name, not the string of the array too.

Also because this command already knows that what you are giving as input is a string, you musn't put a $ in front of the names of your strings. A $ is used to identify something as being a string and replace that part with the contents of the string.
Basically putting a $ in front of it will replace the whole name of the string including the $ with the contents in that string. For example if $test contained "hello" and you were to do global $test, TCL would make of this global hello and that's not what we want, in this case, now do we.

If we would take the set example where we have the array test with the strings "(when)" and "(what)" in it, you also don't have to use the global command like global test(when) test(what), but you can use global test which would give you access to the global strings "(what)" and "(when)" of the array test aswell.
The global command converts entire arrays, not just individual strings.

A final note: the global command is usually put on the first line of the procedure. This is not neccesary, but it's more like an unwritten rule.
It's also easier to overview your script if the global is on the first line rather than somewhere hidden deep inside the procedure.

5.6 Adding up and substracting

With the incr command you can easily add up and substract numbers from strings.
The syntax of an incr command is incr <string> [+/-][number].
Here are a few simple examples to explain the command fast and easy:

#Add 1 up to the string test.
incr test

#Add 2 up to the string test.
incr test 2

#Add 3 up to the string test (the + sign is not required for addition).
incr test +3

#Substract 4 from the string test (the - sign is, logically, required for substraction).
incr test -4

With the incr command you can only add up and substract.
Dividing, multiplying and things like that have to be done with the expr command, but that's something for later on.
Note that the string you are inputting also has to be a valid numeric number or the incr command will return an error.


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


Design & Graphics by Shawn Borton of CalcioStar.com