Warm tip: This article is reproduced from serverfault.com, please click

其他-TCL incr文件名

(其他 - TCL incr filename)

发布于 2020-12-08 05:16:25

我不想设置$ 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"

}

但是,我上面的代码,似乎不起作用?

Questioner
Kimi
Viewed
11
Mkn 2020-12-08 17:42:48

具有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

}