识别文本文件中的空行并在tcl中遍历该列表

时间:2019-07-08 10:28:13

标签: 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

我在catch multiple empty lines in file in tcl中找到了这个,但是我没有得到我想要的。

1 个答案:

答案 0 :(得分:1)

我建议使用数组:

# 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}

ideone demo