如何有效分裂线?

时间:2013-06-24 14:26:57

标签: tcl

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

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

1 个答案:

答案 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}