2011-04-19 83 views

回答

1

你的意思是這樣嗎?

set fi [open "filename"] 
set fo [open "outputfile" w] 
while {[gets $fi line]!=-1} { 
    if {$line!=""} { 
    for {set i 0} {$i<10} {incr i} { 
     puts $fo $line 
    } 
    } 
} 
close $fi 
close $fo 
+1

你不應該'while {![eof $ fi]} {set line [gets $ fi]; ...}'。改爲使用它:'while {[gets $ fi line]!= -1} {...}'。 ''[eof]'只有在你試圖讀取文件末尾之後才返回true **,所以在讀完最後一行後,你需要經過while循環的一次額外迭代。 – 2011-04-19 15:39:48

+0

@Glenn:謝謝你的提示。我會調整我的答案。 – bmk 2011-04-19 18:29:27

+0

如果好= 10,最好= 20,如果這兩個都包含在某個文件中,並且該文件必須被讀取並且輸出應該如下所示///////////我很好-10 am best-20 I – 2011-04-25 08:59:08

1

你的問題還不清楚。

您的意思是包含「好」或「最好」的行需要重複10次?

set fin [open infile r] 
set fout [open outfile w] 
while {[gets $fin line] != -1} { 
    switch -glob -- $line { 
     *good* - 
     *best* { 
      for {set i 1} {$i <= 10} {incr i} { 
       puts $fout $line 
      } 
     } 
     default { 
      # what to do for lines NOT containing "good" or "best"? 
     } 
    } 
} 
close $fin 
close $fout 

如果你指的是實際的話需要重複10次:基於

set str "these goods are good; that bestseller is the best" 
puts [regsub -all {\y(good|best)\y} $str {& & &}] 
# prints: these goods are good good good; that bestseller is the best best best 

更新:

while {[gets $fin line] != -1} { 
    puts $fout [regsub -all {\y(good|best)\y} $line {& & & & & & & & & &}] 
} 

爲regsub命令的一個示例你的意見,你可能想要:

array set values {good 10 best 20} 
while {[gets $fin line] != -1} { 
    regsub -all {\y(good|best)\y} $line {&-$values(&)} line 
    puts $fout [subst -nobackslashes -nocommands $line] 
} 
+0

如果好= 10,最好= 20,如果這兩個都包含在某個文件中,並且該文件必須被讀取並且輸出應如下所示/////// ////我很好 - 上午10點最好 - 20我 – 2011-04-25 09:00:38

+0

只有好的和壞的話應該被拿出來並且分別重複10次和20次////前綴應該是我很好 - 上午10點最好 - 20我同樣如果有多條這樣的線路,那麼前綴應該以我很好 - 上午10點最好 - 我下一條線路被提取等等開始, – 2011-04-25 09:03:58

相關問題