当前位置:网站首页>Detailed explanation of TCL scripting language (1)
Detailed explanation of TCL scripting language (1)
2022-07-23 19:08:00 【qq_ forty-four million nine hundred and eighty-five thousand si】
Introduction to language
TCL It's a very common scripting language , Powerful . It was first called “ Tool command language ”“Tool Command Language”, But that's not what it means now , But we still call it TCL, Pronunciation is "tickle”.TCL It's often used in rapid prototyping , scripting ,GUI And testing .
It actually contains two parts : A language and a library . First ,Tcl It's a simple scripting language , It is mainly used to issue commands to some interactive programs, such as text editors 、 Debugger and shell. It has a simple syntax and strong extensibility ,Tcl You can create new procedures to enhance their built-in command capabilities . secondly ,Tcl It's a library bag , Can be embedded in applications ,Tcl The library contains a parser 、 Routines and for executing built-in commands can enable you to expand ( Define a new process ) Library function . Applications can produce Tcl Command and execute , Commands can be generated by the user , It can also be read from an input of the user interface ( Buttons or menus, etc ). but Tcl After receiving the command, the library decomposes it and executes the built-in command , There are often recursive calls .
Tcl The data type is simple . Yes Tcl Come on , It has only one kind of data to process —— character string .Tcl Store variable values as strings , Don't care about its actual use type . embedded Tk(toolkit) Graphics tools can provide simple and rich graphics functions , So that users can easily create a simple graphical interface .
Tcl The execution is interactive ,Tcl Provides an interactive command interface , There are two interfaces :tclsh and wish.tclsh Only support Tcl command ,wish Support Tcl and Tk command . Through the interactive interface , Then we can execute UNIX shell command , Execute command by command , And get the execution results immediately .
Tcl/Tk Can provide cross platform support .Tcl The language can run in most of today's popular UNIX、WINDOWS and Macintosh Wait for the system , And the command is universal , It's just that the details of the startup are a little different .
Basic grammar
1、** Command structure **:commandName arguments. Each command is passed through line feed or ; separate . for example :
set foo 0
set bar 1;
2、** notes **: Use # notes , However, it should be noted that the following comments are wrong :
set foo0 # This is the note ( error )
because tcl The parser always thinks that a command should end with a newline or semicolon , The others are considered parameters in the same line . So the right thing to do is :
set foo 0; # This is the note
In fact, for people who are used to compiled programming languages, add a at the end of each line ; It should be taken for granted , So we should not be unaccustomed to this .
3、 data type :tcl Forms such as... In other languages are not supported int, double ,char And so on , The only support is string type . That is to say, a variable can be understood as different types at different times .
4、 Variable :tcl Two types of variables can be defined , Variables and arrays .
Variable : stay tcl Variables can be assigned directly without declaration . Examples of assigning values to variables :
set ba 1;
set ma “qian lifeng”; # The function of quotation marks is to tell the compiler to include spaces
The value of the obtained variable can be prefixed with the corresponding variable name $ To obtain , for example :
puts $ba; # Print out ba Value
Array : Arrays do not need to be declared , Direct assignment , for example :
set qian(0) 1;
set qian(1) 2;
An interesting thing is that array subscripts can be written out of order , It's not necessarily a number , Can be a string . for example :
set qian(123) f;
set qian(6789)fs;
set qian(good)yes;
We can use puts $qian(123); To get the contents of the array . Of course tcl Arrays also support multidimensional arrays , The declaration form is :
setqian(1,1,1,1,1) fs; # Any number of dimensions
If we want to view the information of an array that has been assigned , have access to parray command : Such as :
parray qian; # Will print out qian All the information of the array
5、 String manipulation :string The basic syntax of the command is as follows ( there string1,string2 It can be a string or a variable ,option Is one of the following options ).
stringoption string1 string2;
option Operation options for :
compare Compare according to the order of the dictionary . according to string1 <, =, >string2 Return respectively -1, 0, 1
first return string2 For the first time string1 The location of , If not string1 Then return to -1
last and first contrary
trim from string1 Delete the beginning and end of appear in string2 The characters in
tolower return string1 The new string after all characters in are converted to lowercase characters
toupper return string1 All strings in are converted to uppercase strings
length return string1 The length of
Example :
string length$qian; # The variable will be returned qian The length of
6、 Digital operation : because tcl Only one of them string Variable of type , So when you want to operate on numbers ,tcl Provides incr and expr Two operands .
incr The basic usage of is :
incrvariable integer(variable Must be a number )
For example, I want to add a number 3
set a 3;
incr a 3; # take a Add 3, If we want to reduce 3, Then for incr a–3;
default incr a Equate to a++ It means , That is, self increase 1.
expr The basic grammar of is :
expr functionnumber
expr Is a syntax designed to provide more complex operations , For example, multiplication and division, and so on . When performing arithmetic operations, you must put expr Commands are placed before arithmetic operations . for example :
set a 20; set b4;
set c [expr$a/$b]; # At this time c The value of is 5
besides ,expr You can also recognize some functions and their return values as follows :
abs(x) x The absolute value of
round(x) x The integer value obtained after rounding
sin(x) x Sine of
…………………………………..
for instance :
set a [exprsin(0.3)]; # Calculation 0.3 Sine of
7、 escape
stay tcl Can be used in {
} Enclose the string that needs to be escaped . for example :
set a 5;
set b {
$a}; # here b The value of is ”$a“, instead of 5.
8、 flow control
if control flow , Basic grammar :
if {
expression } {
# operation ;
} else {
# Other operations ;
}
It should be noted that if….else, if……elseif… Medium else,elseif And if The one behind the {
On the same line .
switch control flow : Basic grammar :
switch $x {
The number 1 {
operation 1 ;}
The number 2 {
operation 2 ;}
}
9、 loop
tcl Three loop commands are provided , Respectively :for,foreach,while. Besides tcl Two cycle control commands are also provided . Be careful : The opening curly brackets of the program block in the three commands must be on the same line as the three commands .
1、while Basic grammar :while {
Conditions } {
Block }
2、foreach Loop through a set of arguments , And will execute his loop body every time . The basic structure : foreachvariable {
items} {
block}
there variable It's the name of the variable , Easy block Use in . give an example :
foreach element{
0 m n b v} {
switch$element {
# Judge element Value
}
}
3、for Is the most commonly used loop . Its basic structure is :for{
initialization} {
condition} {
increment} {
body}
for example :for {
set i 0} {
$i< 10} {
incr i} {
puts $i;} # Will print out 0 To 9
Program example
#add a member function call "greet"
Class mom
mom instproc greet {
} {
$selfinstvar age_
puts"$age_ year old mom say: How are you doing?"
}
#Create a child class of "mom" called"kid"
#and overide the member function "greet"
Class kid -superclass mom
kid instproc greet {
} {
$selfinstvar age_
puts"$age_ year old kid say: What's up, dude?"
}
#Create a mom and a kid object, set each age
set a [new mom]
$a set age_ 45
set b [new kid]
$b set age_ 15
#Calling member function "greet" of eachobject
$a greet
$b greet
边栏推荐
- Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv
- Three ways to realize multithreading
- Access intranet rds+mysql through SSH
- Google正在改进所有产品中的肤色表现 践行“图像公平”理念
- ROS (27): the simple use of rosparam and the unsuccessful transfer of parameters through launch and its solution
- 【2018】【论文笔记】石墨烯场效应管及【1】——GFETs的种类和原理,GFETs特性,GFETs在太赫兹中的应用和原理
- GVIM/VIM使用技巧
- TCL脚本语言详解(1)
- MQ【MessageQueue 图文详解及四大MQ比较】
- Application of jishili electrometer in testing scheme of new energy battery
猜你喜欢
![[2013] [paper notes] terahertz band nano particle surface enhanced Raman——](/img/09/af80a744573ce53a0056e7124675a8.png)
[2013] [paper notes] terahertz band nano particle surface enhanced Raman——
![Redis [super superfine introductory tutorial]](/img/a6/02b3e670067292d884bab7425b4ffb.png)
Redis [super superfine introductory tutorial]
![Log framework [detailed learning]](/img/2f/2aba5d48e8a544eae0df763d458e84.png)
Log framework [detailed learning]
![[2020] [paper notes] phase change materials and Hypersurfaces——](/img/cc/a69afb3acd4b73a17dbbe95896404d.png)
[2020] [paper notes] phase change materials and Hypersurfaces——

Know two things: how does redis realize inventory deduction and prevent oversold?

Analysis on the implementation of Flink exactly once delivery

国外芯片,为什么有中文资料和网页?

Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv

软件测试岗位就业竞争压力大,985毕业的“打工人“同样存在苦不堪言

FPGA实现IIC协议(一)IIC总线协议
随机推荐
Three ways to realize multithreading
Jumpserver administrator account is locked
Conception de l'interface UART basée sur la FPGA
电子元件-电阻
软件测试岗位就业竞争压力大,985毕业的“打工人“同样存在苦不堪言
398. Random number index hash table method
Completion report of communication software development and Application
Spark 安装与启动
Google is improving the skin color performance in all products and practicing the concept of "image fairness"
【2020】【论文笔记】太赫兹新型探测——太赫兹特性介绍、各种太赫兹探测器
Crack WiFi password with Kail
FPGA实现IIC协议(一)IIC总线协议
Redis [2022 latest interview question]
JUC concurrent programming [detailed explanation and demonstration]
Common problems of sklearn classifier
FPGA implementation of IIC bus of IIC protocol (II) (single read / write drive)
人脸识别系统技术方案
Great God "magic change" airpods, equipped with usb-c interface, 3D printing shell makes maintenance easier
C # split usage, split split split string
MySql【从了解到掌握 一篇就够】