Tcl Learning--Grammar | variables

Source: Internet
Author: User

" Syntax | Variable "

L scripts, commands, and words

TCL provides about 100 commands, and TK provides another dozens of, and the script contains one or more lines separated by a newline character or semicolon.

Set a 12

Set B 23

Can also be written as a line: set a 12; Set B 23

Each command consists of multiple words, separated by a space, and the first letter is the command name, so:

1. Set-word, also command name

2. Word A

3. Word, assign this value to a

L processing Commands

TCL processing commands in two steps: parsing and execution

1) During the parsing phase, TCL carries out a series of simple string operations, such as replacing the input string stored in the $input variable with the

2) in the execution phase, each word in the command has a specific meaning. Tcl takes the first word as the command name, and executes the command according to the arguments passed.

This is illustrated below:


L Replace

Mode one: variable substitution

Example 1: Calculating or assigning a value


Example 2: Create 4 button events for. B1,.B2,. b3,. b4

% foreach num {1 2 3 4} {Button.b$num}

Note:

Substitution does not affect the segmentation of each word in the command, even if the characters that are replaced include spaces, tabs, newline characters, and so on.

mode two: command substitution


Note:

Command substitution is to replace part or all of a word with the result of a command, which is done [] , and the commands in [] are called, such as the expr command above.

method Three: reverse slash substitution

A character that is used to insert a word like \ n, [, $, space characters, which is considered by the TCL parser to have a special meaning, as follows:


Note:

The backslash substitution differs from the general substitution, which is done separately before the TCL interpreter interprets the command, which means that the replacement spaces are treated as word separators unless they are enclosed by "" or {} .

L Double quotation mark reference

TCL provides methods that prevent parsers from processing characters such as $ and semicolons, which are referred to as references.

Example 1: To assign the Hello World to a msg without double quotation marks will be an error.


This is the time to quote:

Example 2: A Space, tab, newline, and semicolon are treated as ordinary characters.


Example 3: Set MSG to a string containing a variable name, the squared information of the variable


L curly Braces Reference

If the double quotation marks are treated in a special way, then the curly braces are a more thorough reference, which cancels the special meaning of all the special characters. All the spaces, tabs, line breaks, and semicolons are treated as normal characters.

Example 1: Print out the above MSG information truthfully


L Parameter Expansion

Tcl passes a list as a parameter to a procedure. Let's look at an example:

Example 1: To delete all files ending in the D:\test directory, all. H.


Like the above is not removed, why? Because the glob command returns a list of files in *.h form, such as A.H, B.h, c.h, d.h, the entire file list is passed as a parameter to the file Delete command, and the deletion fails because A.H is not found. So how do I delete it?

Way One: start with a {*}


Way Two: file names are all listed


method Three: parse with eval command to re-pass it to Filedelete


L Notes

If the first non-whitespace character of a command is #, then this line is treated as a comment. Note: The comment must appear on the first character position where Tcl expects to get the command. If the note appears elsewhere, it is treated as a normal character, as part of a command word.


The second line # appears in the middle of a command, causing the set command to receive 3 parameters, so it is an error. The last # is considered an annotation because it is immediately terminated by a command in the flag; To accurately understand the meaning of the above red, look at the following two examples.

Example 1: The case of non-annotations in {}


All characters in {} are treated as a parameter, assigned to a set specified by a string, so it is not a comment!

Case 2: Comments in {}


There are two {},if commands that treat the first {} as a Boolean expression, and if it is true, it calls the TCL interpreter to process the second argument as a TCL script. When the TCL parser is parsed again, the first line that begins with # is recognized as a comment.

Example 3: Curly braces that appear in comments often result in an error

For another use, use the IF command to annotate multiple lines. See the example below.


In Java, you can use the

/*

*/

In Python, three single-quote pairs, or three double-quote pairs, are used to annotate multiple lines.

Tcl can use the IF command to achieve the same effect: multi-line comment problem.


If 0, the condition is false, the following branch will not go, thus achieving the purpose of the annotation.

L errorinfo

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


After the error, TCL will set the ErrorInfo to a stack, save the exact location of the error, you can use the puts $errorInfo output The value of this variable.

L set command

The command can be used to create, read, and modify, the first parameter is the variable name, and the second argument, if any, is the new value of the variable.


L Append command

This command is used to add a character to the end of the string, characterized by: because this is the internal expression of TCL, processing speed fast!


L Array

An array is a collection of elements, each of which has its own name and value variables. Includes two parts: the array name + the element name in the array. Here are two common ways to define arrays:

mode one:set Arrayname (Element) mode


mode two: array set arrayname{} mode

Note:

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

2. If the elements of the array contain spaces, you need to use \ translation, or variable substitution

L related commands for arrays

command one: array names Arrayname: Returns the list of elements of the specified array

Command two: array size arrayname: How many elements are defined in the array

command three: array exists arrayname: Used to verify that an array exists.

command four: array set and array get: the latter is the dictionary that gets the array

Array traversal: typically use foreach to iterate through an array, for example:

L incr command

This command provides a simple way to change the value of a variable. INCR reads two parameters, namely, the variable name and an integer

8.5 Before the non-existent variables, such as the above variable y, will be an error! But in the 8.5 and later versions, the variable is created, by default +1.

L unset commands and array unset

The unset command is used to destroy variables. You can receive multiple variables, each of which is a variable name, which means that you can delete more than one variable at a time, such as:

Array unset is used to destroy arrays:

L pre-defined variables

The TCL library automatically creates and manages some global variables. The most commonly used sections are as follows:

1. Argv0, argc, argv

2. Env

3. Tcl_platform

L Other variable functions

Tace command : Used to monitor variables, invoke the specified TCL script when the variable is set, read, or deleted. The trace command is useful when debugging, which also allows you to create read-only variables that can be used for "propagation", for example: As long as the value of the variable changes, the values displayed on the database or on the screen are updated immediately.

Global and Upvar command: Can be used by the procedure, access not all of its local variables.

namespace command : Create and manage namespaces, namespaces are named collections of commands and variables. namespaces allow you to separate commands and variables to ensure that they do not interfere with commands and variables in other namespaces.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tcl Learning--Grammar | variables

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.