我想按\t
=
或,
我试着使用:
set line [split $line" \t=,"]
set result {}
foreach element $line {
if { $element != {} } {
lappend result $element
}
}
行看起来像:
name1 name2 name3 floating_point_number1, floating_point_number2
或
name = floating_point_number1, floating_point_number2 ... floating_point_numbern-1, floating_point_numbern
但它似乎很慢。 如何更改代码以使其更有效?
答案 0 :(得分:4)
您可能希望查看tcllib的textutil::split
模块。
% set s "this\tis=a,line=,\twith separators="
this is=a,line=, with separators=
% package require textutil::split
0.7
% textutil::split::splitx $s {[[:blank:]=,]}
this is a line {} {} {with separators} {}
如果它是您想要避免的空元素,那么tcllib struct::list
包有帮助
% package req struct::list
1.7
% set l [textutil::split::splitx $s {[[:blank:]=,]}]
this is a line {} {} {with separators} {}
% struct::list filterfor field $l {[llength $field] > 0}
this is a line {with separators}