2017-03-16 66 views
0

這裏是我試過的foreach輸出到一個文件

foreach_in_collection t1 [ ga ...] { 
    set f1 [ga $t1 type_name] 
    set f2 [ga $t1 bb] 
    puts $f1 
    puts $f2 
    puts $file1 $f1 $f2 
} 

我想寫兩個輸出$ F1的代碼和$ F2至$文件1,請讓我知道 上面所說$文件1 [列表$ F1 $ F2]也沒有工作..

回答

0

試試這個:(前)

set fd [open out.txt w] 
foreach {x y} [list a b c d e] { 
    puts x=$x 
    puts y=$y 
    puts $fd $x 
    puts $fd $y 
} 
close $fd 

輸出文件:

$ cat out.txt 
cat: out.txt: No such file or directory 
$ 

執行輸出:(後)

$ tclsh foo.tcl 
x=a 
y=b 
x=c 
y=d 
x=e 
y= 
$ 

輸出文件:

$ cat out.txt 
a 
b 
c 
d 
e 

$ 
0

沙拉德的解決方案是完美的。如果你想嘗試,還有一種方法。

SBORDOLO-M-V1VG:實驗sbordolo $貓A11

exec rm -rf out1.txt ;# No need to worry about file is there or not. 
exec touch out1.txt 
exec chmod 777 out1.txt ;#You can give a write permission here. I have given 777 just like that 

foreach {x y} [list a b c d e f] { 
    puts x=$x 
    puts y=$y 
    exec echo $x >> out1.txt 
    exec echo $y >> out1.txt 

} 

當你運行該腳本: SBORDOLO-M-V1VG:實驗sbordolo $ tclsh的A11

x=a 
y=b 
x=c 
y=d 
x=e 
y=f 

SBORDOLO-M -V1VG:EXPERIMENT sbordolo $ cat out1.txt

a 
b 
c 
d 
e 
f