TCL incr filename



我不想设置$outfile那么多次,我想增加文件名。如何在TCL做到这一点?

set incr 1
set outfile [open "file$incr.txt" w]
set infile1 "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
set infile2 "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
set infile3 "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
foreach infile $infiles$incr {
puts $oufile$incr "testing"
}

然而,我上面的代码,似乎不起作用?

具有incr函数:

set infile {}
set i 0
lappend infile "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
lappend infile "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
lappend infile "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
foreach value $infile {
incr i
set outfile [open "file${i}.txt" w]
foreach files $value {
puts $outfile "testing"
}
close $outfile
}

另一种解决方案,您可以使用阵列:

set infile(1) "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
set infile(2) "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
set infile(3) "[glob /a/b/c/d/a12b.txt /a/e/c/e/c123d.txt /e/e/d/e/2abd.txt ]"
foreach {key value} [array get infile] {
set outfile [open "file${key}.txt" w]
foreach files $value {
puts $outfile "testing"
}
close $outfile
}

最新更新