【问题标题】:TCL incr filenameTCL incr 文件名
【发布时间】: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"

}

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

【问题讨论】:

  • 你为什么在没有任何通配符的绝对路径上使用glob
  • 任何时候你有变量变量名,考虑使用数组。

标签: tcl filenames


【解决方案1】:

带有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
}

另一种解决方案,您可以使用array

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

}

【讨论】:

  • 在这种情况下,带有数组的选项看起来非常像正确的选择。字典也可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多