[Switch] Introduction to TCL syntax

1. Script composition

 

 

 

set a 2
set b 3
//
set a 2;set b 3

 

A TCL script can include multiple commands. The commands are separated by newlines or semicolons. A command is separated by spaces, such as commands, variables, and other parameters.

 

2. Replacement

 

  • variable substitution

 

  

 

set c $a+$b //c = a+b=2+3

 

Use the dollar sign $ for variable substitution. In the above formula, c=2+3 instead of 5. To make c=5, you need to use command substitution.

 

  • command substitution

 

  

 

set c [expr $a+$b]

 

When [] is used, the first character in parentheses is used as a command, the following are used as command parameters, and the expr command is used for expression evaluation. Return the result of the evaluation and assign it to c.

 

  • backslash \ substitution

 

\ makes special characters lose their special meaning

 

set msg money\ \$3333

 

Using \, the above spaces will not be treated as character separation between commands, and $ will not be treated as variable substitution.

 

  • Double quotes "" and curly braces {}

 

Using double quotes "" and curly braces {} also allows the interpreter to treat special characters such as delimiter substitutions as normal characters. "" just doesn't deal with delimiters, but will still deal with newlines, variable substitution $, and command substitution

 

set y "$a xxx" //y -> 2 xxx

 

Use curly braces to turn all special characters into normal characters

 

set y {/n$x [expr 10 +100]} //y->/n$x [expr 10 +100]

 

3. Notes

 

The '#' character at the beginning of the line indicates the line comment. '#' must appear as the first character of the command expected by the TCL interpreter. Therefore, '#' appears at the beginning of the line and the content after ';' can be regarded as a comment.

 

# this is a comment
set msg "hello world!" ;#this is another comment
set msg2 "nice day!" #error,this is not a comment

 

4. Variables

 

A TCL variable consists of a name and a value, both of which can be arbitrary characters. It should be noted that when '$' is used for variable substitution, it is worth the word symbol between the first characters that are not letters, numbers, or underscores as the name of the variable to be replaced. Use the set command to set variables, and the unset command to unset variables.

 

copy code
set a 2
set a.1 4
set b $a.1
set c ${a.1}
# b=2.1;c=4
copy code

 

TCL's array is a bit like a python dictionary, and the array subscript can be any string.

 

set day(monday) 1
set day(tuesday) 2
set b $day(monday);#b=1

 

unset delete variable

 

unset a b day

 

5. Expression
TCL supports operators similar to C language, and operands similar to C, 0777//octal, 0xff//hexadecimal, 8e10//8 to the 10th power, like C language, TCL is also built-in many mathematical functions.

 

6, list list

 

Lists are an important data structure in TCL. A list represents a collection of elements, and the elements can be any character, including list.

 

{}
{1 2 3 4 5}
{1 23 3 {4 5}}

 

1) list uses the list command to generate a list, list 1 2 3 4

 

2) Use concat list list... to merge two or more lists

 

3) lindex list index returns the index-th element of the list, starting from 0.

 

4) llength list returns the number of elements in the list

 

5) linsert list index value value... returns a new list, the new list is to insert all values ​​before the index element of the list

 

6) lreplace list first last value value... Returns a new list, and replaces the first to last elements of the list with the value parameter. If there is no value parameter, it means deletion.

 

7) lrange list first last Returns the string composed of the first to last elements of the list. If last is end, return to the end of the string

 

8) lappend varname value value Append each value to the back of varname.

 

9) lsearch -exact -glob -regexp list pattern returns the index of the first matched element, or -1 if not found. -exact, -glob, -regexp are optional 3 pattern matching techniques.

 

-exact means exact match; -glob and string match command match in the same way, -regexp means regular expression match.

 

10) lsort options list Sort the list, specify the sorting method through options, there are -ascii: the default method, sorting according to the ASCII character order, -dictionary, according to the lexicographical order, the lexicographical order does not consider case, and the numbers are sorted as integers. -integer, convert the list elements into integers, and sort them according to integers; -real, convert them into floating-point numbers; -increasing ascending order, -decreasing descending order. -command cmd According to the cmd command, compare the elements one by one and give the sorting result.

 

11) split string splitchar Split the string according to splitchar and return the split list

 

12) join list joinstring The list elements are combined into a string, and the middle is separated by a joinstring. The default joinstring is a space.

 

7. Control flow

 

Similar to C language, it supports common control flow, if, switch, while, for, foreach. foreach does a good job of iterating over lists.

 

copy code
if {$x>0}{
    do_something;
}elseif{$x==0} {

...
}else{
    do_anotherthing;
}
    
copy code

 

Use {} to split statement blocks and test conditional expressions. The last { in the first line must be placed on the previous line, otherwise TCL will think that the if command has ended, and treat the { at the beginning of the line as a command and cause an error.

 

'if {$x>0}'There is a space between if and {.

 

switch options string {pattern body pattern body} //options can specify the matching method -exact,-glob(default),-regexp

 

copy code
switch $x {
a -
b {incr t1}
c {incr t2}
default {incr t3}
}
copy code

 

while test body

 

copy code
set i [expr [llength $a] -1]
while { $i>=0}{
lappend b [lindex $a $i]
incr i -1
}
copy code

 

for init test reinit body

 

for {set i [expr [llength $a] -1]} {$i>=0} {incr i -1} {
lappend b [lindex $a $i] }

 

foreach safe leaf body

 

set x {}
foreach {i j} {a b c d e f} {
lappend x $j $i
}

 

Similar to C, TCL provides break and continue commands for breaking out of loops.
source command, TCL provides source command to call and execute other tcl scripts, source /home/xxx/scripts/scripts1001.tcl

 

8. Function definition

 

TCL uses the procedure proc to define functions similar to C. Using proc, you can implement some custom commands, and the usage is the same as the built-in commands.

 

proc add {x y } {expr $x+$y}
add 1 2 ;# result is 3

 

You can use global to declare a global variable inside a proc, that is, the variable is defined outside the proc. After declaring global, you can access the global variable inside the proc.
Apply upvar, use upvar to implement a reference to a variable, upvar othervar myvar, once the binding of the reference is implemented, the modifications to myvar will correspond to othervar. 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325317385&siteId=291194637
TCL