Tcl Syntax Introduction 1

Source: Internet
Author: User
Tags glob mathematical functions sorts

1, script composition

23//23

Tcl scripts can include multiple commands, separated by line breaks or semicolons, and a command that uses spaces to separate 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, c=2+3 instead of 5, to let c=5 need to use command substitution.

    • Command substitution

  

Set c [expr $a + $b]

When [] is used, the first character in the parentheses is treated as a command, followed by the command argument, and the expr command is used to evaluate an expression. Returns the evaluation result and assigns a value to C.

    • Backslash \ Permutation

\ causes special characters to lose special meanings

Set msg money\ \$3333

Using \, the above spaces will not be treated as inter-command character splitting, and $ will not be substituted as a variable.

    • Double quote "" with curly braces {}

Using double quotes "" and braces {} can also allow the interpreter to treat special characters such as delimiter substitutions as normal characters. "" Just do not handle the delimiter, the line break, the variable substitution $, the command permutation will still be processed

" $a xxx " 2 XXX

Use curly braces to turn all special characters into normal characters

Ten + +

3, Notes

The ' # ' character at the beginning of the line begins to represent the behavior comment. ' # ' must appear in the TCL interpreter to expect the first character of the command. So ' # ' appears at the beginning of the line and '; ' Later content can be used as a comment.

#"Hello world! " ; #"Niceday! " #Error not a comment

4, variable

Tcl variables consist of a name and a value, and both names and values can be any character. Note that the ' $ ' variable substitution is worth the first word symbol that is not a letter, number, or underscore character as the substituted variable name. Use the SET command to set the variable, and the unset command cancels the variable.

2 set A. 1 4 set B $a. 1 set C ${a. 1 }# b=2.1; c=4

The TCL array is somewhat to the Python dictionary, and the array subscript can be any string.

12set B $day (Monday); #b =1

unset Deleting a variable

Unset a B Day

5, expression
TCL supports similar C language operators, and C similar operands, 0777//octal, 0xff//16, 8e10//8 10 times, and C language, Tcl also built up a lot of mathematical functions.

6, List

The list is an important data structure for TCL. A list represents a collection of elements that can be any character, including a list.

{}{12345}{13 {  45}}

1) list is used to generate lists by using the ListBox listing, List 1 2 3 4

2) Use Concat list List ... Merge two or more lists

3) lindex list index Returns the list index element, which is calculated starting at 0.

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

5) Linsert list index value value ... Returns a new list that inserts all value before the list index element

6) lreplace list First Last value value ... Returns the new list, replacing the list first to last element with the value parameter, without the value parameter, which means delete.

7) Lrange list first returns a string that consists of the second and last elements of the list. Last is end returns to the end of the string

8) Lappend VarName value value attaches each value back to varname.

9) Lsearch-exact-glob-regexp list pattern returns the first matching element index, and returns-1 if it is not found. -exact,-glob, and-regexp are optional 3-mode matching techniques.

The-exact represents an exact match, and the-glob and string match commands are matched in the same way that-regexp represents a regular expression match.

Lsort Options list Sorts the list by options, with-ascii: default, sorted by ASCII character order,-dictionary, in dictionary order, dictionary order regardless of case, numbers are sorted as integers. -integer, converts the list element to an integer, sorts by integers,-real, converts to floating-point numbers,-increasing Ascending,-decreasing descending. -command CMD gives the ordered result after comparing the elements by the cmd command.

Split string Splitchar splits the string according to Splitchar, returning the split list

Join list joinstring merges the list elements into a string, separated by joinstring, and the default joinstring is a space.

7, Control flow

Similar to the C language, supports common control flows, If,switch,while,for,foreach. foreach can perform a good traversal of the list.

if {$x >0}{    do_something;} ElseIf {$x = =0} {...} Else {    do_anotherthing;}    

Use {} to split the statement block and the conditional expression test, the last {must be placed on the previous line, or TCL will assume that the IF command has ended, and the beginning of the {as a command resulting in an error.

' If {$x >0} ' if and {there are spaces between.

Switch options String {pattern body pattern body}//options can specify a match-exact,-glob (default),-regexp

-B {incr T1}c {incr t2}default {incr t3}}

While Test body

set I [expr [llength $a]-1] while {$i >=0-1

For init test reinit body

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

foreach Varnem list body

Similar to C, TCL provides the break,continue command for jumping out of a loop.
The source command, TCL provides the source command to invoke and execute other TCL scripts, SOURCE/HOME/XXX/SCRIPTS/SCRIPTS1001.TCL

8, function definition

TCL uses the process proc to perform C-like function definitions, using proc to implement custom commands that are used in the same way as built-in commands.

Proc Add {x y} {expr $x + $y}
Add 1 2; # result is 3

Proc Internal can use global to declare a global variable, that is, the variable is defined outside the proc body, 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 reference binding is implemented, the changes to MyVar will correspond to OtherVar.

Tcl Syntax Introduction 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.