在 Tcl 脚本中转换列表中的列 [英] Converting Columns in a List in Tcl Script
问题描述
我想使用 Tcl 脚本将文件的一列转换为列表.我有一个文件名input.dat",其中包含两列数据,如下所示:
I want to convert a column of a file in to list using Tcl Script. I have a file names "input.dat" with the data in two columns as follows:
7 0
9 9
0 2
2 1
3 4
我想将第一列转换成一个列表,我编写了如下的 Tcl 脚本:
And I want to convert the first column into a list and I wrote the Tcl Script as follows:
set input [open "input.dat" r]
set data [read $input]
set values [list]
foreach line [split $data \n] {
lappend values [lindex [split $line " "] 0]
}
puts "$values"
close $input
结果显示为:7 9 0 2 3 {} {}
The result shows as: 7 9 0 2 3 {} {}
现在,我的问题是这两个额外的{}"是什么,我的脚本中的错误是什么,因为它产生了两个额外的{}",我该如何解决这个问题?
Now, my question is what is these two extra "{}" and what is the error in my script because of that it's producing two extra "{}" and How can I solve this problem?
有人可以帮我吗?
推荐答案
除非间歇性空字符串对您的程序任务具有重要意义,否则您还可以使用从 Tcl 列表(带有空字符串元素)到字符串的转换修剪空字符串元素(在末端和中间):
Unless intermittent empty strings carry some meaning important to your program's task, you may also use a transformation from a Tcl list (with empty-string elements) to a string that prunes empty-string elements (at the ends, and in-between):
concat {*}[split $data "\n"]
这篇关于在 Tcl 脚本中转换列表中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!