Tcl learning-syntax | variable, tcl-syntax variable

Source: Internet
Author: User

Tcl learning-syntax | variable, tcl-syntax variable

【Syntax | variable]

LScripts, commands, and words

Tcl provides about 100 commands and Tk provides dozens of other commands. scripts contain one or more commands separated by line breaks or semicolons.

Set a 12

Set B 23

You can also write a row: set a 12; set B 23

Each Command is composed of multiple words separated by spaces. The first letter is the command name. Therefore:

1. set-> word, also the command name

2. a-> word

3. 12-> assign the value to

LProcessing Command

The Tcl command is parsed and executed in two steps.

1) in the parsing phase, Tcl performs a series of simple string operations, such as replacing the input string stored in the $ input variable

2) In the execution phase, each word in the Command has a specific meaning. Tcl uses the first word as the command name and runs the Command Based on the passed parameters.

The figure is as follows:


LReplace

Method 1: replace variables

Example 1: calculation or assignment


Example 2: create four button events:. b1,. b2,. b3,. b4

% Foreach num {1 2 3 4} {button. B $ num}

Note:

Replacement does not affect the separation of each word in the command, even if the replaced characters include spaces, tabs, line breaks, and other blank characters, it does not affect.

Method 2: Command replacement


Note:

Replacing a command is to replace part or all of a word with the result of a command. If you use [], the command in [] will be called, such as the expr command above.

Method 3: backslash replacement

It is used to insert characters such as \ n, [, $, and space characters into words that are considered to have special meanings by the Tcl parser, as shown below:


Note:

The backslash replacement is different from the general replacement. This replacement must be performed independently before the Tcl interpreter interprets the command. This means that the replacement space character will be viewed as a word separator, unless they are included by "" or.

LDouble quotation marks

Tcl provides some methods to prevent the parser from processing characters such as $ and Semicolon. These methods are called references.

Example 1: If you want to assign hello world to msg without double quotation marks, an error is returned.


In this case, you need to reference:

 

Example 2: Process spaces, tabs, line breaks, and semicolons as common characters.


Example 3: Set msg to a string containing the variable name, square of the variable, and other information


LBraces reference

If the double quotation mark (\ n) is used for special processing, braces are used as a more thorough reference, which will remove the special meaning of all special characters. All spaces, tabs, line breaks, and semicolons are processed as common characters.

Example 1: print the above msg information truthfully


LExpand Parameters

Tcl will pass a list as a parameter to a process. Let's take a look at an example:

Example 1: Delete all files ending with. h In the Directory d: \ test.


As you can't delete it, why? Because the glob command returns a line *. file List in the form of h, such as. h, B. h, c. h, d. h. The entire file list is passed to the file delete command as a parameter because. h, B. h, c. h, d. h. deletion failed. so how should we delete it?

Method 1: Add {*} at the beginning {*}


Method 2: List all file names


Method 3: Use the eval command to parse and send it to filedelete again.


LNote

If the first non-blank character of a command is #, this line is considered as a comment. Note: The annotator must appear at the first character of the command to be obtained by Tcl. If a comment appears elsewhere, it is considered a common character and a part of a command word.


The second line # appears in the middle of a command, causing the set command to receive three parameters, so the error occurs. The last # is considered as a annotator because it is followed by the termination of a command. To accurately understand the meaning of the above marked Red, let's look at the two examples below.

Example 1: {} non-Comments


All characters in {} are considered as a parameter and assigned to the set variable as a string, so it is not a comment!

Example 2: Notes in {}


The if command treats the first {} As a boolean expression. if it is true, it calls the Tcl interpreter to process the second parameter as a Tcl script. When the Tcl parser parses again, the first line starting with # is recognized as a comment.

Example 3: braces in comments often cause errors.

Another purpose is to use the if command to annotate multiple lines. See the following example.


It can be used in Java

/*

*/

In Python, use three single quotes or three double quotes to comment multiple lines.

In Tcl, the if command can be used to achieve the same effect: multi-line comments.


If 0, that is, If the condition is false, the branch below will not go, so as to achieve the purpose of annotation.

LErrorInfo

When an exception is returned, the error message is saved in the global variable errorInfo,


When an error occurs, Tcl sets errorInfo as a stack and stores the exact location of the error. You can use puts $ errorInfo to output the value of this variable.

LSet command

This command can be used to create, read, and modify a variable. The first parameter is the variable name, and the second parameter is the new value of the variable if there is something in it.


LAppend Command

This command is used to add a character at the end of the string. Features: because this is an internal expression of Tcl, it can be processed quickly!


LArray

An array is a collection of elements. Each element has its own name and value variables. It consists of two parts: array name + element name in the array. Below are two common array definitions:

Method 1: set arrayName (element)


Method 2: array set arrayName {}


Note:

1. In Tcl, arrays are unordered data structures (stored as hash tables), while lists are ordered.

2. If the element of the array contains spaces, use \ translation or replace the variable.


LArray-related commands

Command 1: array names arrayName: returns the list of elements in the specified array.


Command 2: array size arrayName: number of elements defined in the array


Command 3: array exists arrayName: used to check whether an array exists.


Command 4: array set and array get: the latter is used to obtain the array dictionary.


Array traversal: Generally, foreach is used to traverse arrays. For example:


LIncr command

This command provides a simple method to change the value of a variable. incr reads two parameters: the variable name and an integer.


Before 8.5, an error is reported for a non-existent variable, such as the preceding variable y! However, this variable is created in versions 8.5 and later. The default value is + 1.

LUnsetCommand and array unset

The unset command is used to destroy variables. Multiple variables can be received. Each parameter is a variable name, which means that multiple variables can be deleted at a time. For example:


Array unset is used to destroy Arrays:


LPredefined Variables

The Tcl library automatically creates and manages some global variables. The most common parts are as follows:

1. argv0, argc, argv


2. env


3. tcl_platform



LOther Variable Functions

Command: it is used to monitor variables. The specified Tcl script is called when variables are set, read, or deleted. The Trace command is useful during debugging. This variable also allows the creation of read-only variables and can be used to "spread" variables. For example, if the variable value changes, the value displayed on the database or screen is updated instantly.

Global and upvar commands: they can be used by the process to access all its local variables.

Namespace command: create and manage namespaces. namespace is a set of commands and variables. Namespaces can separate commands and variables to ensure that they do not interfere with commands and variables in other namespaces.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.