Tcl language learning - the basics

First, the script, command and word symbols

A TCL script can contain one or more commands. It must be separated with a newline or semicolon between the commands.

1. Keyword / Tag

Base program variables are
variables: the variable name, the value of the variable
the variable name requirements: Any string can be used as a variable name, case sensitive
variable value requirements: Any string can be used as the variable value

⦁ variable assignment basic syntax:
the SET varname value;
EG:
the SET the X-10;
the SET text "the Hello, World"

2, Replacement

TCL interpreter in the analysis of command, all the command parameters are viewed as a string, for example,

set x 10; # define the variable x, and x is assigned the value 10

set y x + 100; # define the variable y, the value of y is x + 100, 110 rather than we expect

  In one embodiment the second command, x is regarded as part of a string of x + 100, if we want to use the value of x '10', it is necessary to tell the interpreter TCL: here we expect that the value of the variable x instead of the character 'x'. TCL interpreter how to tell it, which use the replacement function provided in TCL language.

TCL offers three forms of replacement : variable substitution, command substitution, and backslash substitution. Each displacement will cause one or more single itself be replaced by other values. Substitutions may occur in every word, including the command name, and replacement can be nested

the X-10 the SET
⦁ variable displacement

Variable displacement marked by a $ symbol

set y $ x + 100; #y is 10 + 100, where x is replaced with the value 10;

⦁ command substitution

It is replaced by the command [] enclosed TCL commands and their parameters

set y [expr $ x + 100]; #y value of 110;

The value of y is 110, where when the TCL interpreter encounters a character '[', then it will expr as a command name of that activates expr corresponding to C / C ++ process, and the 'expr' and variable displacement +100 obtained after '10 'is passed to the command processing process.

Note: [] must be a valid TCL script length limitation. [] Value of script a return value of the last command, for example:

set y [expr $ x + 100; set b 300]; # y is 300, because the return value is set b 300 300

⦁ backslash replacement

set y \ $ x + 100; #y value of $ x + 100;

⦁ release variable:
unset the X-;

3. double quotation marks and braces

  TCL provides two other methods so that the interpreter and the delimiter character substitutions special characters such as an ordinary character, without special treatment, it is necessary to use double quotes and braces ({}).

TCL interpreter double quotes various delimiter are not processed, but for line breaks and $ and [] symbols will be different replacement process as usual. E.g

set x 100

Y SET "$ X ddd"; #  Y value of 100 ddd

While in braces, all special characters will become ordinary characters, lose their special significance, TCL interpreter does not make them special treatment.

set y {/ n $ x [expr 10 +100]}; #y value / n $ x [expr 10 +100]

4. Notes and output

  The TCL is a comment character '#', '#', and where all characters until the end of the line as a comment are TCL, TCL interpreter annotation will without any treatment.

However, note that the "#" in place TCL interpreter desired command the first character must appear appear, was treated as a comment. E.g:

set  a 100 # Not a commnet

The above command is not a comment, it will error

set a 100; # this is a comment

Above is a valid comment

  The first command in the "#" character is not treated as a comment, as it appears in the middle of the command line, TCL interpreter and the characters following it as a command parameter processing, resulting in an error. The second command in the "#" will be treated as a comment, because the previous command has been used for the end of a semicolon, TCL interpreter expect next command then appears, there is now this position "#", then the character was as annotated.

puts: output text, such as a plurality of words are separated by a space or require the use of TAB ""} or {enclosed; a plurality of write commands in a row; interval.

correctly puts Hello #      

puts Hello, World; # correct, not by the space between multiple words separated or TAB

puts Hello World; # run command line error, separated by a space

puts "Hello, World - In quotes" ;    # 注释

puts {Hello, World - In Braces} 

puts "This is line 1"; puts "this is line 2"; # correctly, two commands separated by semicolons

puts "Hello, World; - With a semicolon inside the quotes"; # correctly, semicolon in double quotes, as part of a string

Second, the data type

tcl actually only one data type: string

In tcl variable type conversion or not match the type of problem, all the "data" are in the form of a string, then how tcl numerical calculation? How to generate a decimal?

Numeral Calculations:

Numerical tcl support various mathematical functions, but can only be used in the command expr

eg:

set angle 30;

set radian [expr $angle*3.1415926/180.0];

set value [expr sin($radian)];

value is 0.499999992265;

Output data type

Format output data to a file or the screen, format command can be used to control the display to ensure that the required output.

eg:

set vlaue1 [format %.1f $value];

value1 value of 0.5;

Third, the data structure -List

  List data structure is one of a kind most tcl language functions to the often very powerful and flexible application, a list can have any number of elements, each element can be any string, the simplest is to include a list of any number of space, tab, newline character string any number of elements spaced apart.

eg:

john anne mary jim;

Create a list

set x [list 1 2 3 4];

Specified delimiter string into a list

set string “1,2,3,4”;

set x [split $string “,”];

One element taken from the list

set value1 [lindex $x 0];

set value2 [lindex $x 1];

set value3 [lindex $x 2];

set value4 [lindex $x 3];

Fourth, the data structure -Array

  In Tcl, in addition to simple variables, it also provides an array, the array is geometric elements, each element has its own name and value. The array consists of two parts, i.e., the array name and the element name, the name of the array, and the array element name can be any string, so in the course must be a distinction space, like double-quote character.

Creating an array

set student (name) "San";

set student(age) 15;

set student(gender) “男”;

Query array

array size student; student number of elements in the array in the query

array names student; student inquiries array element name

Multidimensional Arrays

set grade(jim,chinese,english) [list 98 99];

set grade(peter,chinese,english) [list 96 100];

 Fifth, the control structure

  There are many types of control structures, including conditionals, loops, exception handling, processing conditions underlying the like.

Common control statements

Explanation

if {expression} {body1} else {body2};

if {expression1} {body1} elseif {expression2} {body2}

Conditional execution, where else or else if not necessarily exist

foreach value valuelist {body}

valuelist is a linked list data, the command value through each of valuelist, each performed once while traversing the body

switch $var {a {body1};b {body2};c {body3}}

A comparison value with many patterns, the pattern matching can be performed corresponding to the body

for {init} {test} {reinit} {body}

The init as the initial amount of the test as the expression is true if the body is executed, and then repeat reinit as the initial amount and repeat the procedure.

while {test} {body}

The test as an expression processing, if it is true, is performed body, then this process is repeated test procedure

return

The end of the return value of a function value

break

For the innermost loop termination

continue

Terminate the current iteration step for the innermost loop, the next iteration of the command

catch {body}

Command exception handling when there is no abnormality is the return value is 0, otherwise it returns a non-zero value

Sixth, program structure

source: tcl can be an external file execution unfold here

global: specify certain variables are globally available. In tcl, the main program where the variable is not globally available, you must use the global statement can be invoked in the course of the program or in the process of years.

proc: a sub-described process, followed by the procedure name, the parameter list, the executable. It should be noted that the list of parameters and process parameters should call at the same. The exception is that, if a write-only parameters of the process, it can receive a plurality of inlets in the form of list of parameters.

proc explain:

1. Format: proc name args body

2. Call the method parameters can be curly brackets or double quotes may not contain

3. In the puts commands in need of replacement, the need to use brackets

Examples: 012_proc.tcl

proc sum {arg1 arg2} {

  set x [expr $arg1+$arg2];

  return $x                           ;#过程返回值

  }

puts " The sum of 2 + 3 is: [sum 2 3]\n\n"    ;#调用过程

#puts " The sum of 2 + 3 is: [sum {2 3}]\n\n"  ;#出错,提示找不到第二个参数,置换过程中第一个参数是{2 3},所以找不到第二个参数

puts " The sum of 2 + 3 is: sum(2 3)\n\n"      ;#输出sum(2 3),因为没有方括号,根本没有进行置换

puts " The sum of 2 + 3 is: sum{2 3}\n\n"      ;#输出sum{2 3},因为没有方括号,根本没有进行置换

sum 2 3               ;#正确

sum {2} {3}           ;#正确

sum "2" "3"            ;#正确

过程PROC的参数定义

讲解:

1.  过程的参数赋缺省值:proc name {arg1 {arg2 value}}

2.  过程的不确定个数的参数定义:proc name {arg1 args}

proc example {first {second ""} args} {    ;#参数定义:赋缺省值和不确定个数参数定义

  if {$second == ""} {

    puts "There is only one argument and it is: $first";

    return 1;

    } else {

    if {$args == ""} {

      puts "There are two arguments - $first and $second";

      return 2;

      } else {

      puts "There are many arguments - $first and $second and $args";

      return "many";

      }

    }

  }

set count1 [example ONE]

set count2 [example ONE TWO]

set count3 [example ONE TWO THREE ]

set count4 [example ONE TWO THREE FOUR]

puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"

Guess you like

Origin www.cnblogs.com/hypersonic-gycrazy/p/11077856.html
TCL