tcl [5]:list大全

1. list的基本操作:创建、访问、计长、分割

#>>>>>>>>>list>>>>>>>
# list 可由数字,字符,字符串或者别的list组成
# <1> 创建一个list
set x "a b c"
# 结果:a b c
# <2> 通过index(0...)取list的某一个值,如取list x的第3个值
lindex $x 2
puts "item at index 2 of the list {$x} is : [lindex $x 2]\n"
# 结果:item at index 2 of the list {a b c} is : c
# <3> 获取list的长度
llength $x 
# 结果:3
# <4> 分割list,用split list "<符号>"
set y [split 1/2/4434/dfdf "/"]
# 结果:1 2 4434 dfdf
# > 下面两个list是不同的,通过花括号括起来的属于list的一个元素
set b [list a b {c d e} {f {g h}}]
# a b {c d e} {f {g h}}
# > 而通过split分隔开(默认是空格)是独立的元素
set a [split "a b {c d e} {f {g h}}"]
# a b \{c d e\} \{f \{g h\}\}
lindex $b 0
# a
lindex $a 0
# a
lindex $b 3
# f {g h}
lindex $a 3
#d
# > 两个list的index = 3的元素即可证明。

# <5> 分清哪个是list的item !!!
set str x/y/z/144
# 结果:x/y/z/144
# > 创建一个str为”x/y/z/144“
lindex [split $str "/"] 2
# 结果:z
# > 将原来的str根据符号”/“分割,本质上是创建了第二个list: "x y z 144",所以lindex ... 2 取到的是z
lindex [split $str "/"] 3
# 结果:144
puts "[split $str "/"]"
# 结果: x y z 144
puts "$str"
# 结果: x/y/z/144
# >打印两个list来证明

# <6> 循环打印list的每一个元素
# 注意:这里的变量j是从list $x中逐个取值,即从list $x中逐个取值然后赋值给j
# 需要和C语言for循环中的i区分
set i 0
foreach j $x {
	puts "$j is item number $i in list x"
	incr i
}
# 结果
# a is item number 0 in list x
# b is item number 1 in list x
# c is item number 2 in list x

# <7> foreach的高能用法
# >1< 从list $y中一次取两个变量
set y "1 2 3 4 5"
# 1 2 3 4 5
foreach {a b} $y {
       puts "$a - $b"
}
# 结果
# 1 - 2
# 3 - 4
# 5 -

# <7> foreach的高能用法
# >2< 两个list同时遍历
foreach a $x b $y {
    puts "a=$a, b=$b"
}
# 结果
#a=a, b=1
#a=b, b=2
#a=c, b=3
#a=, b=4
#a=, b=5

2. list 增加元素、删减元素、替换元素、连接list

# adding and deleting members of a list
set b [list a b {c d e} {f {g h}}]
a b {c d e} {f {g h}}
set b [split "a b {c d e} {f {g h}}"]
a b \{c d e\} \{f \{g h\}\}
# >concat arg1 arg2 ...
set a [concat a b {c d e} {f {g h}}]
a b c d e f {g h}
# >>>concat可以将args给连接起来,它会自动消除args之间的{},除非{}里面还有一个{}
# > lappend listName arg1 arg2...
set x [list 1 2 3]
1 2 3
set y [lappend x "4 5 6"]
1 2 3 {4 5 6}
puts "$x"
1 2 3 {4 5 6}
set y [lappend x "4" "5" "6"]
1 2 3 {4 5 6} 4 5 6
# >>> lappend会在原来的list的基础上添加字符串

# > linsert listName index arg ...
set y [linsert $x 3 "xy"]
1 2 3 xy {4 5 6} 4 5 6
# >>> linsert 替代掉原来的list $x 中index=3的元素替换为”xy"

# > lreplace listName first last arg ...
set x [list a b c d e f h]
a b c d e f h
set y [lreplace $x 3 5 "1 2"]
a b c {1 2} h
% lindex $y 3
# >>> lreplace将list $x的第3-5个元素替换为“1 2”

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#应用案例:设立两个list,实现两列表相乘
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
set x [list 1 2 3]
set y [list 4 5 6]

set emptylist [list ]
#设置一个空的list去让计算结果append

foreach j $x {
        foreach k $y {
                lappend emptylist [expr {$j*$k}]
              
        }
}

puts "$emptylist"
# 4 5 6 8 10 12 12 15 18
puts "[lindex $emptylist 0]"
# 4

#逐个打印
for {set i 0} {$i < 9} {incr i} {
	puts "emptylist\[$i\] = [lindex $emptylist $i]"
}

# emptylist[0] = 4
# emptylist[1] = 5
# emptylist[2] = 6
# emptylist[3] = 8
# emptylist[4] = 10
# emptylist[5] = 12
# emptylist[6] = 12
# emptylist[7] = 15
# emptylist[8] = 18

3. list 寻找元素、元素排序、区间获取元素、对比元素

# lsearch, lsort, lrange
# >>>>>>>>>>>>>>>>>
set list [list {washington 1789} {adams 1797} {jefferson 1801} {masison 1809} {monroe 1817}]
# {washington 1789} {adams 1797} {jefferson 1801} {masison 1809} {monroe 1817}
set x [lsearch $list washington*]
#0
incr x
#1
set y [lsearch $list masison*]
#3
set sublist [lrange $list $x $y]
#{adams 1797} {jefferson 1801} {masison 1809}
set sublist [lsort $sublist]
#{adams 1797} {jefferson 1801} {masison 1809}

# glob 通配符
# * -> 任意字符,只要前面的字符符合就返回1
# ? -> 一个字符(任意),只要出现一个就返回1,出现2个就返回0
# \x ->转义,是为了比较 *,?符号
string match f?? foo
#1
string match \* f**k
#1
string match f foo
#0
string match f f
#1

# string subcommands - length index range
# > 子命令,为了避免模糊的命令取名
string length "this is"
#7 => 返回string "this is"的长度
string index "this is" 5
#i => 返回index=5的string的字符
string range "this is" 0 3
#this => 返回range 从0-3的字符

# String comparisons - compare match first last wordend
# >>>>操作文本比较实用
set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"
set relativepath "CVS/Entries"
set directorypath "/usr/bin/"

set paths [list $fullpath $relativepath $directorypath]

foreach path $paths  {
    set first [string first "/" $path]
    set last [string last "/" $path]

    # Report whether path is absolute or relative

    if {$first != 0} {
        puts "$path is a relative path"
    } else {
        puts "$path is an absolute path"
    }

    # If "/" is not the last character in $path, report the last word.
    # else, remove the last "/", and find the next to last "/", and
    #   report the last word.

    incr last
    if {$last != [string length $path]} {
        set name [string range $path $last end]
        puts "The file referenced in $path is $name"
    } else {
        incr last -2;
		#退回两个,刚好退回到“/”前一个字符,因为last本身是落在“/”,先是增加1,后来再减2,就退回到了“/”前面的字符了。
        set tmp [string range $path 0 $last]
        set last [string last "/" $tmp]
        incr last;
        set name [string range $tmp $last end]
        puts "The final directory in $path is $name"
    }

    # CVS is a directory created by the CVS source code control system.
    #

    if {[string match "*CVS*" $path]} {
        puts "$path is part of the source code control tree"
    }

    # Compare to "a" to determine whether the first char is upper or lower case
    set comparison [string  compare $name "a"]
    if {$comparison >= 0} {
        puts "$name starts with a lowercase letter\n"
		# string compare只比较字母顺序,而不是比多少,ASCII码大写字母在小写字母之前。所以如果>最小的小写字母,那么字符就是小写,否则就是大写
    } else {
        puts "$name starts with an uppercase letter\n"
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值