识别文本文件中的空行,并在tcl中循环该列表
我有一个文件,其中包含以下类型的数据识别文本文件中的空行,并在tcl中循环该列表,tcl,Tcl,我有一个文件,其中包含以下类型的数据 A 1 2 3 B 2 2 2 c 2 4 5 d 4 5 6 从上面的文件中,我想执行一个循环 三次迭代,其中第一次迭代包含A、B元素,第二次迭代包含c元素,第三次迭代包含d元素。所以我的html表看起来像 Week1 | week2 | week3 ---------------------------- A 1 2 3 | c 2 4 5 | d 4 5 6 B 2 2 2 我在SO中找到了这个,但我没有得到我想要的。我
A 1 2 3
B 2 2 2
c 2 4 5
d 4 5 6
从上面的文件中,我想执行一个循环
三次迭代,其中第一次迭代包含A、B元素,第二次迭代包含c元素,第三次迭代包含d元素。所以我的html表看起来像
Week1 | week2 | week3
----------------------------
A 1 2 3 | c 2 4 5 | d 4 5 6
B 2 2 2
我在SO中找到了这个,但我没有得到我想要的。我建议使用数组:
# Counter
set week 1
# Create file channel
set file [open filename.txt r]
# Read file contents line by line and store the line in the varialbe called $line
while {[gets $file line] != -1} {
if {$line != ""} {
# if line not empty, add line to current array with counter $week
lappend Week($week) $line
} else {
# else, increment week number
incr week
}
}
# close file channel
close $file
# print Week array
parray Week
# Week(1) = {A 1 2 3} {B 2 2 2}
# Week(2) = {c 2 4 5}
# Week(3) = {d 4 5 6}
我会用
struct::matrix
来做这类事情,尤其是报告
包可以很容易地处理打印。嗨,Jerry,谢谢你的解决方案。你能添加一些注释行来理解吗more@Aroon我希望这能有所帮助