4.SDC---TCL基础知识

puts "Hello, World!";#这是一条注释
puts {Hello, World!}
  • 这里的每条语句都会向标准输出设备打印"Hello,World!"。用空格分隔的单词被视为是语句中的多个参数。
puts Hello World!

将给出一个错误,指示无法处理参数。错误信息为:cannot find a channel named ‘Hello’


  • TCL具有多种语言结构,它们是:
    • 变量
    • 列表
    • 表达式与运算符
    • 控制流语句
    • 过程

1.Tcl变量

set abc"1234";#这里set是命令,abc是变量,1234是分配给变量的值

如果需要对变量求值,则需要使用美元($)符号。例如,puts $abc将会打印1234。
set $def(1) 4567;这里def是数组变量,索引被设置为1
set $def(test) 5678;这里索引是test

puts $def(1):#有效命令
puts $def(test);#有效命令
puts $def; 无效命令
  • 如果要尝试利用另一个命令设置变量,则应将该命令括在方括号中。方括号中的任何内容都会在使用前求值。然而,大括号内的方括号中的内容不进行求值
set x [set y 100];  #将x的值设置为100
set x {[set y 100]}; #将x的值设置为[set y 100]

2.TCL 列表

  • TCL中的列表是对象的集合。像任何列表一样,可以对列表进行添加,对列表进行索引,并在列表中进行搜索。
# The following creates a list
set gates [list AND OR NOT NAND NOR]
set gates {{AND} {OR} {NOT} {NAND} {NOR}]
set gates [split “AND.OR.NOT.NAND.NOR” “.”]

# To add another item to a list, use lappend
set gates [lappend gates XOR]

# To search within a list, use lsearch. This returns the matching indices of the list
# Returns 2, which is the index in the list
puts [lsearch $gates NOT];

# Returns -1, since there is no match found
puts [lsearch $gates XNOR];

3.Tcl表达式与运算符

  • Tcl中使用expr命令来对表达式求值。
set x 10;
# Both statement will return the value of 30
expr $x + 20
expr {$x + 20}
  • 两条expr命令将返回相同的值。但是,Tcl建议表达式使用大括号(braces),因为这有助于程序更快地执行。
    在这里插入图片描述
    在这里插入图片描述

4.Tcl的控制流语句

  • Tcl控制流语句由以下种类的结构组成:
    • 列表遍历—Iterating over lists
    • 决策—Decision making
    • 循环—Loops
    • 子程序—Subroutines

4.1 列表遍历—Iterating over lists

  • 为了遍历列表,Tcl提供了foreach结构。

set gates [list AND OR NOT NAND NOR XOR]
set index 1
foreach element $gates {
puts “Gate $index in the list is $element”
incr index; #This increments the index
}


This will generate the output as:
Gate 1 in the list is AND
Gate 2 in the list is OR
Gate 3 in the list is NOT
Gate 4 in the list is NAND
Gate 5 in the list is NOR
Gate 6 in the list is XOR
  • Tcl的foreach为用户提供了同时遍历多个列表的独特功能。

set allgates {}
foreach gatelist1 {AND OR XOR} gatelist2 {NAND NOR XNOR} {
lappend allgates $gatelist2 $gatelist1
}
puts $allgates

这段语句将在变量allgates中存储以下值"NAND AND NOR OR XNOR XOR"。可以看到,这个遍历程序可以让来自不同列表的项目混合在一起。


4.2 决策—Decision making

  • Tcl提供if-elseif-else结构来提供决策能力。
if { $frequency < 330 } {
puts "Chip will function well, but slower than expected"
} elseif { $frequency > 330 } {
puts “Chip will not function"
} else { puts “Chip will function optimally" }

  • It is important to use the braces {} carefully. Each segment has its own pair of braces.

4.3 循环—Loops

  • 当程序想在一个条件下循环和终止时,可以使用Tcl提供的for和while语句。它还提供了两个额外的结构"break"和"continue"。Break用于提前结束循环,而continue则用于停止执行当前循环的代码,并重新评估循环条件。在遍历列表时请注意,break和continue也可以与foreach一起使用。
for <initial value> <condition> <next step> {
<statement body>
}
while <condition> {
<statement body>
}

4.4 Tcl过程—Tcl Proceduresc

  • Tcl过程使用proc来编写。使用return语句从过程中返回值。
proc sum {addend1 addend2} {
set value [expr {$addend1 + $addend2}]
return $value
}
# Calling the procedure
set x [sum 5 10]
puts $x # Will print 15

在这里插入图片描述


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值