tcl语言学习记录1

1、双引号和花括号被用来将多个单词组织成一个变元。双引号允许在分组中出现替换操作。花括号阻止替换发生。这种规则适用于命令、变量和反斜杠替换。

#!/usr/bin/tcl/tcl8.6.9/unix/tclsh
set s Hello
puts stdout "The length of $s is [string length $s]."
#The length of hello is 5.
puts stdout {The length of $s is [string length $s].}
#The length of $s is [string length $s].

2、方括号没有分组作用,其为嵌套命令,通常被认为是当前分组的一部分。

puts stdout "The length of $s is [string length $s]."

3、过程:类似于C中定义一个函数。但是要注意花括号的使用风格,位于第一行结尾处的花括号标志proc第三个变元的开始,也就是命令体。该花括号一定要以此格式书写。

#!/usr/bin/tcl/tcl8.6.9/unix/tclsh
proc Diag {a b} {
  set c [expr sqrt($a *$a +$b * $b)]
  return $c
}
puts "The diagonal of a 3, 4 right triangle is [Diag 3 4]"

4、利用tcl实现阶乘:

#!/usr/bin/tcl/tcl8.6.9/unix/tclsh

#while循环
proc Factorial {x} {
  set i 1; set product 1
  while {$i <= $x} {
    set product [expr $product * $i]
    incr i
  }
  return $product
}

puts stdout [Factorial 10]

#递归方式
proc Factorial1 {x} {
  if {$x > 1} {
    return [expr $x * [Factorial1 [expr $x-1]]]
  } else {
     return 1
  }
}
puts stdout [Factorial1 10]

5、Tcl解释器在默认情况下,假定变量名只包含字母、数字和下划线。

unset:删除变量

发布了78 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/hepiaopiao_wemedia/article/details/100567282
今日推荐