51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2202|回复: 0
打印 上一主题 下一主题

TCL几条命令小结

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-5-26 11:21:25 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
Set
1.对变量进行赋值操作
2.变量的值已经发生改变
Append
1.增加字符串 跟在append后面的变量不能使用变量置换符号
如只能使用:
append txt “sldfkjlsjfl”或append txt skjdflksjdflsjkdf
不能使用:
Append $txt “sldfkjlsjfl”
2.变量的值已经发生改变

Incr
1.增加整型变量 跟在incr后面的变量不能使用变量置换符号
如只能使用:
incr  int +3
不能使用:
Incr  $ int +3
2.使用incr时后面跟的是正负数而不是加减法
3.变量的值已经发生改变
expr
把此命令放在这个位置应该不对
1.因为expr操作的是表达式,因些此处使用的变量一定要使用$(变量转换符)来进行使用
2.注意使用后变量本身的值没有改变

总结
        因此在使用变量的时候比如说for语句,或需要变量自增的时候建议使用“incr var +-setp“或”set var [expr var+setp]”
        凡是使用了$变量转换符的都不会改变其值,除非在使用set变量.
        有点像高级语言中的引用和值调用
        包换之后使用的lreplace命令如下面的例子:
        Set lista {2 3 4 5 6 7 8 9 9 10}
        Set j 2
        if {[lindex $lista $j]<[lindex $lista [expr $j+1]]} {   
                set temp [lindex $lista $j]                             
                lreplace $lista $j $j [lindex $lista [expr $j+1]]
                lreplace $lista [expr $j+1] [expr $j+1] $temp                
        }            
        puts "after arrary $lista"


        实现方法如下:
set lista {2 3 4 5 6 7 8 9 9 10}
        set j 2
        if {[lindex $lista $j]<[lindex $lista [expr $j+1]]} {   
                set temp [lindex $lista $j]                             
                set lista [lreplace $lista $j $j [lindex $lista [expr $j+1]] ]
                set lista [lreplace $lista [expr $j+1] [expr $j+1] $temp                 ]
        }            
        puts "after arrary $lista"
附上使用list和数组实现冒泡算法的例子
proc bubble { lista } {
       
        set temp {}
        for { set i 0 } { $i<9 } { incr i +1 } {
                for {set j 0} { $j<[expr 9-$i] } { incr j +1 } {
                        if {[lindex $lista $j]<[lindex $lista [expr $j+1]]} {
                                set temp [lindex $lista $j]
                                set lista [lreplace $lista $j $j [lindex $lista [expr $j+1]]]
                                set lista [lreplace $lista [expr $j+1] [expr $j+1] $temp]                                
                        }
                }
        }
        puts $lista
}

set lista {10 2 3 4 5 1 7 8 9 0}
bubble $lista

使用数组法:

proc mapao { lista } {
       
        set length [llength $lista]
        for { set temp 0 } { $temp<$length } { incr temp +1 } {
                set arrey($temp) [lindex $lista $temp]
        }
        puts "the array"
        puts $arrey
       
               set arreytemp 0
               for { set i 0 } { $i<[expr $length-1] } {incr i +1} {
                       for { set j 0 } { $j<[expr [expr $length-1]-$i] } {incr j +1} {
                               if {$arrey($j)<$arrey([expr $j+1])} {
                                       set arreytemp $arrey($j)
                                       set arrey($j) $arrey([expr $j+1])
                                       set arrey([expr $j+1]) $arreytemp
                               }
                       }
               }
               for { set temp 0 } { $temp<$length} { incr temp +1 } {
                       append listb $arrey($temp) { }
               }
              
               puts $listb
}      

set lista {1 2 3 4 5 6 19 3 6 7 8 9}
mapao $lista
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-21 16:31 , Processed in 0.070379 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表