1

我想用 \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

但它似乎很慢。如何更改代码以更有效?

4

1 回答 1

4

您可能想查看textutil::splittcllib 中的模块。

% 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} {}

如果它是您想要避免的空元素,那么 tcllibstruct::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}
于 2013-06-24T14:49:05.310 回答