2013-06-24 34 views
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 

但這是eem很慢。 如何更改代碼以使其更有效?

+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} 
+0

將'\ t'更改爲'[:blank:]'以匹配任何水平空格。 –