使用单词以分号结尾的列表?
Working with lists that have words ending in semi-colons?
有没有办法将以分号结尾的字符串(单词)添加到列表中,而不是它本身是单项列表?
我正在处理字符串并将它们分解成单词;并且标点符号通常作为后缀附加。我需要双向表达,即带标点符号和不带标点符号。
是否可以简单地让它成为列表中的列表,并且只引用所有单词,就好像它们是单项列表一样?它似乎有效。或者有更好的方法吗?
谢谢。
set list { a; b c d }
chan puts stdout $list; # a; b c d
set new "b;"
lset list 1 $new
chan puts stdout $list; # {a;} {b;} c d
chan puts stdout [lindex [lindex $list 1] 0]; # b;
chan puts stdout [lindex [lindex $list 3] 0]; # d
chan puts stdout [lindex $new 0]; # b;
I'm working with strings and breaking them into words
在此步骤中使用 split
很重要,除此之外,您可以通过附加到生成的列表来使用它。各种列表命令将确保特殊字符(您的 ;
)不会被错误解释。
从 Tcl 的角度来看,单元素列表 {a;}
和原子字符串 a;
之间没有区别。引用 Tclers' Wiki:
No program can tell the difference between the string "a" and the
one-element list "a", because the one-element list "a" is the string
"a".
不要让花括号把你搞糊涂了。
有没有办法将以分号结尾的字符串(单词)添加到列表中,而不是它本身是单项列表?
我正在处理字符串并将它们分解成单词;并且标点符号通常作为后缀附加。我需要双向表达,即带标点符号和不带标点符号。
是否可以简单地让它成为列表中的列表,并且只引用所有单词,就好像它们是单项列表一样?它似乎有效。或者有更好的方法吗?
谢谢。
set list { a; b c d }
chan puts stdout $list; # a; b c d
set new "b;"
lset list 1 $new
chan puts stdout $list; # {a;} {b;} c d
chan puts stdout [lindex [lindex $list 1] 0]; # b;
chan puts stdout [lindex [lindex $list 3] 0]; # d
chan puts stdout [lindex $new 0]; # b;
I'm working with strings and breaking them into words
在此步骤中使用 split
很重要,除此之外,您可以通过附加到生成的列表来使用它。各种列表命令将确保特殊字符(您的 ;
)不会被错误解释。
从 Tcl 的角度来看,单元素列表 {a;}
和原子字符串 a;
之间没有区别。引用 Tclers' Wiki:
No program can tell the difference between the string "a" and the one-element list "a", because the one-element list "a" is the string "a".
不要让花括号把你搞糊涂了。