TCL命令学习

来源:互联网 发布:js日期选择器插件 编辑:程序博客网 时间:2024/05/16 04:53

upvar  --引用;用于函数传参

array    set    arrayName   list      设置数组arrayName的元素的值。 list 的形式和array   get的
返回值的list形式一样。如果arrayName不存在,那么生成arrayName。

array   names    arrayName   ?pattern?     这个命令返回数组arrayName中和模式pattern匹配的
元素的名字组成的一个list。如果没有pattern参数,那么返回所有元素。如果数组中没有匹
配的元素或者arrayName不是一个数组的名字,返回一个空字符串。

The upvar command provides a general mechanism for accessing variables outside the
context of a procedure. It can be used to access either global variables or local variables in
some other active procedure. Most often it is used to implement call-by-reference argu-
ment passing.

 

% set yearTotal 0
0
% foreach month {Jan Feb Mar Apr May Jun Jul Aug Sep \}


}
wrong # args: should be "foreach varList list ?varList list ...? command"
% foreach month {Jan Feb Mar Apr May Jun Jul Aug Sep \
           Oct Nov Dec} {
           set yearTotal [expr $yearTotal+$earings($month)]
}
can't read "earings(Jan)": no such variable
% set yearTotal 0
0
% foreach month {Jan Feb Mar Apr May Jun Jul Aug Sep \
Oct Nov Dec} {
set yearTotal [expr $yearTotal+$earnings($month)]
}
can't read "earnings(Jan)": no such variable
% set x 43
43
% incr x 12
55
% incr x
56
% set msg ""
% foreach i {1 2 3 4 5} {
      append msg "$i squared is [expr $i*$i]\n"
}
% set msg
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25

%
%
% append x $piece
can't read "piece": no such variable
% append x piece
56piece
% set a(1) 5
5
% set a(2) 6
6
% set a(3) 7
7
% set a(4) 8
8
% set a
can't read "a": variable is array
% array set a
wrong # args: should be "array set arrayName list"
% array set b {1 2 3 4}
% set b(1)
2
% set b(0)
can't read "b(0)": no such element in array
% set b(2)
can't read "b(2)": no such element in array
% puts $b(0)
can't read "b(0)": no such element in array
% puts $b(1)
2
% puts $b(2)
can't read "b(2)": no such element in array
% puts $b(3)
4
% array set {1 a 2 b 3 c 4 d}
wrong # args: should be "array set arrayName list"
% array set c {1 a 2 b 3 c 4 d}
% puts $c(1)
a
% puts $c(2)
b
% puts $c(3)
c
% puts $c(4)
d
% proc printVars {} {
    global a b
    puts "a is $a, b is $b"
}
% set a 3
can't set "a": variable is array
% set x 3
3
% set y 4
4
% printVars
can't read "a": variable is array
% proc printVars {} {
    global x y
    puts "a is $x, b is $y"
}
% printVars
a is 3, b is 4
% proc inc {value {increment 1}} {
     expr $value+$increment
}
% inc 3
4
% proc parray name {
   upvar $name a
   foreach el [lsort [array name a]] {
      puts "$el=$a($el)"
   }
}
% set xx {a c d e 1}
a c d e 1
% parray xx
% set info(age) 37
37
% set info(position) "Vice President"
Vice President
% parray info
age=37
position=Vice President
% parray a
1=5
2=6
3=7
4=8
% array name a
4 1 2 3
% array name info
position age
%

原创粉丝点击