识别文本文件中的空行并使用 tcl 中的列表循环
Identify the empty line in text file and loop over with that list in tcl
我有一个包含以下类型数据的文件
A 1 2 3
B 2 2 2
c 2 4 5
d 4 5 6
从上面的文件我想执行一个循环,
三次迭代,其中第一次迭代将包含 A、B 元素,第二次迭代包含 c 元素,第三次迭代包含 d。这样我的 html table 就会像
Week1 | week2 | week3
----------------------------
A 1 2 3 | c 2 4 5 | d 4 5 6
B 2 2 2
我在 SO catch multiple empty lines in file in tcl 中找到了这个,但我没有得到我真正想要的。
我建议使用数组:
# 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}
我有一个包含以下类型数据的文件
A 1 2 3
B 2 2 2
c 2 4 5
d 4 5 6
从上面的文件我想执行一个循环,
三次迭代,其中第一次迭代将包含 A、B 元素,第二次迭代包含 c 元素,第三次迭代包含 d。这样我的 html table 就会像
Week1 | week2 | week3
----------------------------
A 1 2 3 | c 2 4 5 | d 4 5 6
B 2 2 2
我在 SO catch multiple empty lines in file in tcl 中找到了这个,但我没有得到我真正想要的。
我建议使用数组:
# 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}