Variable values are accessed by the $ symbol, also known as variable exchange.
Tcl is a typical "weak type definition" language, which means that any type can be stored in any variable. For example, the same variable can store numbers, dates, strings or even another Tcl script.
Example 1. 1:
Set fire to John
My name is $foo
Output: Hi, my name is John.
Example 1.2:
Set the second month
Set the third day
Set year 97
Set date $ month: $ day: $ year
Put $date
Output: 2:3:97
Example 1.3:
Set foo and put hi
eval $foo
Output: Hi
In this example, the variable foo stores another Tcl script.
express
Including mathematical expressions and relational expressions, expr command is usually used.
Example 2. 1:
Expression 0 == 1
Output: 0
Example 2.2:
The expression 1 == 1
Output: 1
Compare two numbers, the true output is 1, and the false output is 0.
Example 2.3:
Express 4+5
Output: 9
Example 2.4:
Express new (2)
Output: 0.909297
Command transmission
Replace some Tcl commands with operation results.
Example 3. 1:
Suppose I am [expr 10 * 2] years old and my IQ is [expr 100-25].
Output: I am 20 years old and have an IQ of 75.
Square brackets are signs of command transmission.
Example 3.2:
Set my height to 6.0.
If I were 2 inches taller, I would be [expr $ my _ height+(2.0/12.0)] feet tall.
Output: If I were 2 inches taller, my height would be 6. 16667 feet.
Command flow control
Tcl has a judgment flow (if-else;; Switch) and loop control (while for; foreach)
Example 4. 1:
Set my _ Planet Earth
If {$my_planet == earth} {
Makes me feel at home.
} elseif {$my_planet == venus} {
It means this is not my home.
} Otherwise {
I am neither from the earth nor from Venus.
}
Set the temperature to 95℃
If {$ temp & lt80} {
I feel a little cold.
} Otherwise {
It is warm enough for me.
}
Output:
I feel at home.
It is warm enough for me.
Example 4.2:
Set the number of legs 4
Switch $num_legs {
2 {It may be human. }
4 {think it may be a cow. }
6 {Think it may be an ant. }
8 {thinks it may be a spider. }
The default {place it can be anything. }
}
Output:
It may be a cow.
Example 4.3:
For {set i 0} {$ i <10} {increment i 1} {
Put it in the for loop, i == $i i.
}
Output:
In the for loop, i == 0.
In the for loop, i == 1
In the for loop, i == 2.
In the for loop, i == 3.
In the for loop, i == 4.
In the for loop, i == 5.
In the for loop, i == 6.
In the for loop, i == 7.
In the for loop, i == 8.
In the for loop, i == 9.
Example 4.4:
Set i 0
while { $ i & lt 10} {
Put it in the while loop, i == $i i.
Increment i 1
}
Output:
In a while loop, i == 0
In a while loop, i == 1
In the while loop, i == 2
In the while loop, i == 3
In the while loop, i == 4
In the while loop, i == 5
In the while loop, i == 6
In the while loop, i == 7
In the while loop, i == 8
In the while loop, i == 9
Example 4.5:
Foreach vowel {a e i o u} {
The vowel with $ is a vowel.
}
Output:
A is a vowel letter.
E is a vowel.
I is a vowel.
O is a vowel.
U is a vowel letter.
procedure