In this chapter you will learn how to run a set of commands only under certain conditions.
This is also one of the most important things, there is almost no script, or program for that matter, that
doesn't use this.
With this you can verify user input for instance to see if it was valid or ask what should be done.
With the if command you can make the script perform a set of commands only when something you specify
is equal or not to something else.
This can be a command that has to be performed, two strings, practically anything.
I don't know how I can explain this very well in words, but it's a simple command once you understand it so I'll
just tell you how it works and give some examples to explain it later on.
The syntax of an if command is if { <action> <compare method> <action>} { <body> }.
The two <action>'s are what the if command must compare to each other.
This can be two strings, a command (don't forgot to enclose it with brackets like was told in
Chapter 3.3) or some text (enclosed in quotes like also
told in Chapter 3.3) or anything else that can be used
to match something against it.
The <compare method> tells the if command how to compare the two actions with each other.
This can be either two things, namely == for 'is equal to' or != for 'is not equal to', when one of the
<action>'s is not a number.
If both <action>'s are a number you can also use the 'is greater than' (>) and the 'is less than' (<) signs
to see whether something is larger than the other (you can for example use as <action>'s two commands that
calculate how many characters there are in a string which both result in a number).
Last but not least, by putting an 'is' (=) sign after the 'is greater than' or 'is less than' sign you can
change 'is greater/less than' into 'is greater/less than OR is the same amount as'.
The <body> is basicly the commands you want the if command to execute.
The body doesn't have to be on one line, that's why the if command starts with an open-brace.
You can put a new command on each new line and it'll still be part of the if command until you close
the body up with a close-brace just like with a procedure.
Sometimes you might want to only check if something is true or false.
In computer programs 0 always stands for false and 1 for true. You can do if {$one == 1} { <body> },
but there also is an other way to do the same thing.
To see if something is true you can also simply do if {$one} { <body> } and instead of
if {$one == 0} { <body> } you can do if {!$one} { <body> }. You can replace the string with a
command or anything else.
Inputting only one <action> in the if command without a <compare method> makes it check if it is
true. By adding the ! in front of the <action> without giving a <compare method> or a second <action> it checks
if it is false.
Note that in this case 'true' equals any number above 0, not just 1.
Besides matching only one or two items you can also set multiple conditions.
After the second action you can additionally add either && for 'and' or || for 'or' and put in another
check. After that you can do the same thing again and again and so on, but this doesn't mean it will work yet
when you are using both &&'s and ||'s.
To prevent the if command from making a mistake when you use ||'s and &&'s in the same if
command, you can put the parts you want the && and || to react on between parentheses.
I don't know a better way to say this, so I'll just give an example.
When you have if {$test(start) == $test(stop) && $test(when) != "" || $test(what) != ""} { <body> }
and you want the if command to work only when [ $test(start) == $test(stop) ] and
[ $test(when) != "" || $test(what) != "" ], this proberbly wouldn't work.
The if command would proberbly see this as [ $test(start) == $test(stop) && $test(when) != "" ] or
[ $test(what) != "" ].
This is fixed by putting the last part between parentheses which would make the command look like:
if {$test(start) == $test(stop) && ($test(when) != "" || $test(what) != "")} { <body> }.
Experimenting with this is the best way to figure out how it exactly works.
It is also possible to put a second if command after your first if command.
This can be used for instance when you first want to check string A and if it doesn't comply with your
conditions you want it to check string B.
The way to do this is by putting elseif after your closing brace.
What comes after elseif works in exactly the same way as the normal if command.
A small example:
if {$nick == "SomeNick"} {
<first body>
} elseif {$chan == "#eggdrop"} {
<second body>
}
In this case if $nick equals "SomeNick" <first body> will be executed.
The commands in <second body> will be ignored even if $chan equals "#eggdrop". However, if $nick would not
equal "SomeNick", but $chan would equal "#eggdrop" than <second body> will be executed. If neither of the
strings match nothing will be executed and the script will move on.
You can put in as many elseif commands as you like. After the first one a
second one can be placed and so on.
Besides elseif you can also use else.
The body within else is executed when all of your if and elseif checks fail.
You can use else without having elseif in your if command.
Here are two small examples:
if {$nick=="SomeNick"} {
(...)
} elseif {$chan=="#eggdrop"} {
(...)
} elseif {$host != "*!eggdrop@eggheads.org"} {
(...)
} else {
(...)
}
if {$enabled} {
(...)
} else {
(...)
}
Unlike elseif, you can use else only once per if command.
Now lets expand the small script you made in the previous chapter a bit.
Make the script work for only two channels which you can set at the beginning of the script in arrays.
If the channel matches one of those two let the bot auto-voice the user and if it doesn't than only send the
welcome message to the channel.
If you want an example you can look here: example-ch7.tcl.
|