我如何发送此值
24.215729
24.815729
25.055134
27.123499
27.159186
28.843474
28.877798
28.877798
到tcl输入参数? 如你所知,我们不能使用管道命令,因为tcl dosent以这种方式接受! 我该怎么做才能将这些数字存储在tcl文件中(这些数字在变量中的计数,可以是0到N,在本例中是7)
答案 0 :(得分:2)
在bash中这很容易,将值列表转储到文件中然后运行:
tclsh myscript.tcl $(< datafilename)
然后可以在脚本中使用参数变量访问这些值:
puts $argc; # This is a count of all values
puts $argv; # This is a list containing all the arguments
答案 1 :(得分:1)
您可以使用
等命令读取通过stdin
传送的数据
set data [gets stdin]
或来自临时文件,如果您愿意。例如,以下程序的第一部分(来自wiki.tcl.tk的示例)从文件中读取一些数据,然后另一部分从stdin
读取数据。要测试它,将代码放入一个文件(例如reading.tcl
),使其可执行,创建一个小文件somefile
,并通过例如
./reading.tcl < somefile
#!/usr/bin/tclsh
# Slurp up a data file
set fsize [file size "somefile"]
set fp [open "somefile" r]
set data [read $fp $fsize]
close $fp
puts "Here is file contents:"
puts $data
puts "\nHere is from stdin:"
set momo [read stdin $fsize]
puts $momo
答案 2 :(得分:1)
我在编码时使用的一种技术是将数据作为文字放在我的脚本中:
set values {
24.215729
24.815729
25.055134
27.123499
27.159186
28.843474
28.877798
28.877798
}
现在我可以使用foreach
将它们一次一个地输入命令,或者将它们作为单个参数发送:
# One argument
TheCommand $values
# Iterating
foreach v $values {
TheCommand $v
}
一旦你的代码使用文字,将其切换为从文件中提取数据非常简单。您只需用代码替换文字来读取文件:
set f [open "the/data.txt"]
set values [read $f]
close $f
您也可以从stdin中提取数据:
set values [read stdin]
如果有很多值(例如,超过10-20MB),那么您最好一次处理一行数据。以下是从stdin中读取的方法......
while {[gets stdin v] >= 0} {
TheCommand $v
}