Tcl Tutorial 笔记2 · set ““ {} [] \
目录
- 变量赋值
- set
- Example
- 替换 Substitutions ""
- Backslashes \
- Example2
- 替换 Substitutions {}
- Example3
- 替换 Substitutions []
- Example4
变量赋值
set
set varName ?value?
- If value is specified, then the contents of the variable varName are set equal to value.
- If varName consists only of alphanumeric characters, and no parentheses, it is a scalar variable.
- If varName has the form varName(index), it is a member of an associative array.
Example
set Y 1.25 ;#变量Y赋值为1.25#打印变量Y 的值
puts $Yset str "this is a string"
puts $str
替换 Substitutions “”
使用{} 和 “” 都可以将其内部的多个参数,字符组合成一个参数。但是{}与"“存在这差异。在下面将会有介绍。
1、”"
将单词分组在双引号中,这样就可以在引号中进行替换——或者用更花哨的术语来说,就是“插值”。然后替换的组作为单个参数进行计算。因此,在puts命令中
set varName "12 dollars"
puts "The current stock value is $varName"
在上面的例子中我们可以看到,在""中的$varName作为一个变量的替换,最终输出的结果是
The current stock value is 12 dollars
Backslashes \
通常,\禁止替换紧跟在\后面的字符。
但是下面这些作为转义字符,可以被替换为不同的特定的含义。
在文本末尾的 \ 表示忽略换行。
Example2
set Z Albany
set Z_LABEL "The capital of New York is: "puts "$Z_LABEL $Z" ;# Prints the value of Z
puts "$Z_LABEL \$Z" ;# Prints literal $Z instead of the value of Zputs "\nBen Franklin is on the \$100.00 bill"set a 100.00
puts "Washington is not on the $a bill" ;# Not what you want
puts "Lincoln is not on the $$a bill" ;# This is OK
puts "Hamilton is not on the \$a bill" ;# Not what you want either
puts "Ben Franklin is on the \$$a bill" ;# But, this is OKputs "This string comes out\
on a single line"
替换 Substitutions {}
前面提到了在替换中{} 与 “” 有着不同的行为。下面这里通过几个实际的例子展示{} 与 “” 在tcl解释器中的不同。
1 、在{}中无法实现变量的替换,也就是说,在{}中$varName 将无法替换为前面使用set varName lala中定义的变量的值lala,在{}中还是显示$varName
2、如果字符串已经分组了,用引号或大括号,并且大括号出现在分组的字符串的中间(即"foo{bar"),那么大括号被当作正则字符处理,没有特殊意义。
3、在文本的末尾的 \ 依旧表示不换行。
Example3
set Z Albany
set Z_LABEL "The Capitol of New York is: "puts "\n.............. examples of differences between \" and \{"
puts "$Z_LABEL $Z"
puts {$Z_LABEL $Z}puts "\n....... examples of differences in nesting \{ and \" "
puts "$Z_LABEL {$Z}"
puts {Who said, "What this country needs is a good $0.05 cigar!"?}puts "\n.............. examples of escape strings"
puts {Note: no substitutions done within braces \n \r \x0a \f \v}
puts {But:
The escaped newline at the end of a\
string is replaced by a space}
Resulting output
.............. examples of differences between " and {
The Capitol of New York is: Albany
$Z_LABEL $Z....... examples of differences in nesting { and "
The Capitol of New York is: {Albany}
Who said, "What this country needs is a good $0.05 cigar!"?.............. examples of escape strings
Note: no substitutions done within braces \n \r \x0a \f \v
But:
The escaped newline at the end of a string is replaced by a space
替换 Substitutions []
puts [readsensor [selectsensor]]
- The parser scans the entire command, and sees that there is a command substitution to perform: readsensor selectsensor , which is sent to the interpreter for evaluation.
- The parser once again finds a command to be evaluated and substituted, selectsensor
- The fictitious selectsensor command is evaluated, and it presumably returns a sensor to read.
- At this point, readsensor has a sensor to read, and the readsensor command is evaluated.
- Finally, the value of readsensor is passed on back to the puts command, which prints the output to the screen.
The exceptions to this rule are as follows:
- A square bracket that is escaped with a \ is considered as a literal square bracket.
- A square bracket within braces is not modified during the substitution phase.
Example4
set x abc
puts "A simple substitution: $x\n"set y [set x "def"]
puts "Remember that set returns the new value of the variable:"
puts " X: $x Y: $y\n"set z {[set x "String within quotes within braces"]}
puts "Note curly braces: $z\n"set a "[set x {String within braces within quotes}]"
puts "See how the set is executed: $a"
puts "\$x is: $x\n"set b "\[set y {This is a string within braces within quotes}]"
# Note the \ escapes the bracket, and must be doubled to be a
# literal character in double quotes
puts "Note the \\ escapes the bracket:\n \$b is: $b"
puts "\$y is: $y"
Resulting output
A simple substitution: abcRemember that set returns the new value of the variable:X: def Y: defNote curly braces: [set x "String within quotes within braces"]See how the set is executed: String within braces within quotes
$x is: String within braces within quotesNote the \ escapes the bracket:$b is: [set y {This is a string within braces within quotes}]
$y is: def
Tcl Tutorial 笔记2 · set ““ {} [] \相关推荐
- Tcl Tutorial 笔记1 · 输出
目录 Simple Text Output tcl的注释 输出 puts 参考 Simple Text Output tcl的注释 命令行后进行注释使用 ;# puts "Hello, Wo ...
- Tcl Tutorial 笔记10 · list
在tcl 中列表是一个集合:列表里可以是数字,字符,字符串,或者是其他的列表. 创建列表的几种方法: 1.手动创建列表型变量 set lst {{item 1} {item 2} {item 3}} ...
- Tcl Tutorial 笔记 ·ubuntu命令行运行tcl 命令
ubuntu 安装tcl : sudo apt-get install tcl 在ubuntu命令行运行tcl 命令: usually via running "tclsh" or ...
- Tcl Tutorial 笔记9 · proc 参数传递与return
目录 proc的参数 example1 example2 proc的参数 可以proc传递固定数量的参数,也可以传递数量的可变的参数传递给proc,传递给proc的参数,可以设值默认值,如需要将传递的 ...
- Tcl Tutorial 笔记8 · proc
目录 proc Example Advanced usage proc 在tcl中,proc与在其他的编程语言中的函数是没有差别的. proc 命令创建一个新命令.proc命令的语法是: proc n ...
- Tcl Tutorial 笔记7 ·for incr
目录 for 与 incr for 语法 Example incr Example for 与 incr for 语法 for start test next body 在这里 start 是一个对变 ...
- Tcl Tutorial 笔记6 ·while
目录 while 语法 while 语法 while test {body} 在这里test 作为一个表达式,if test 条件成立将执行body中的内容,如果不成立将跳出该循环. 另外注意 tes ...
- Tcl Tutorial 笔记3 ·math
目录 expr Operators 数字上的操作 数学函数 Type conversions 数组 example Example 1 Example 2 未完待续... expr 用于进行数学计算的 ...
- Tcl Tutorial 笔记5 ·switch
目录 switch 语法规则 example 参考资料 switch 语法规则 switch ?options? string {pattern1 {body1}?pattern2 {body2}?. ...
最新文章
- RDKit | 基于RDKit输出分子结构图(Image)的方法
- 算法笔记_218:花朵数(Java)
- 2K17能力值上90的11位球员,你怎么看?
- sockaddr与 sockaddr_in
- C++对C的加强之新增Bool类型关键字
- 如何用数学和化学方法测量英国海岸线的长度
- AcWing:3.完全背包问题
- 发布一个Easy Linux 版本,正式命名为EzLinux
- Spring Boot Initilizr - 使用Spring Boot CLI
- 【英语学习】【WOTD】raddled 释义/词源/示例
- 深入玩转K8S之智能化的业务弹性伸缩和滚动更新操作
- mysql新手常见问题_MySQL数据库入门-新手常见问题答疑
- 转录组学分析之基因芯片的预处理
- Axure原型工具Axure RP9安装及Licensee
- 如何在android studio中调用mumu模拟器
- OPC:服务器开发(一)如何开发OPC Server
- 曾国藩家书-修身篇 致诸弟·明师益友虚心请教
- 时间最优轨迹规划(3-5-3次多项式)
- 使用poi来导入具有合并单元格的excel表格
- Makefile 与 GCC G++ 入门
热门文章
- 反序列化对象列表发生异常_面试官:你知道Java对象的序列化与反序列化背后的原理吗?...
- excel导出_学习笔记—— 前端导出excel
- python 录制网易云登陆_小白都能看懂:Python爬取网易云音乐下载教程
- Mybatis与JDBC的对比超详细笔记
- java每轮排序结果,冒泡排序及其优化java
- 爱卡创誓记java饰品,《创誓记AIKA》芙兰精灵配上框架眼睛折服宅男宅女
- 归档日志存在arch_还在用ELK? 是时候了解一下轻量化日志服务Loki了
- les有学计算机的吗,赵欢喜
- rc6 java_NativeScript Telerik UI RC6
- matlab拉格朗日曲线_数学中高耸的金字塔——拉格朗日