2016-04-14 6 views
1

Ich schreibe mehrere Zeilen in eine Datei in tcl. Obwohl es mir gelungen ist zu schreiben, wollte ich aber auch nach einigen Zeilen eine Leerzeile haben und für jede Zeile einen Kommentar hinzufügen.mehrere Zeilen in eine Datei in tcl schreiben, wie leere Zeile und Kommentare hinzufügen

Hier geht mein Code.

set tmpdir "set_var tmpdir $tmpdir_path" 
set vdd "set vdd $voltage" 
set gnd "set gnd 0.0" 
set temp "set temp $temperature" 
set rundir "set topdir $topdir" 




set filename "char_run.tcl" 
set fileId [open $filename "w"] 
puts $fileId $tmpdir 
puts $fileId $vdd 
puts $fileId $gnd 
puts $fileId $rundir 
close $fileId 

Bitte schlagen Sie vor, wie Sie für jede Zeile Leerzeilen und Kommentare hinzufügen.

+0

Wenn Sie eine Tcl-Datei zu schreiben, gehen, ich schlage vor, mit 'list' statt String-Verkettung, z.B. 'puts $ fileId [Liste set_var tmpdir $ tmpdir_path'. Dies erzeugt einen gültigen Tcl-Befehl, der nicht erstickt, wenn der Pfad beispielsweise ein Leerzeichen enthält. –

Antwort

1
puts $fileId "$tmpdir\t;# a comment and a blank line\n" 

puts $fileId "$tmpdir\n# a comment on its own line and then a blank line\n" 

puts $fileId "# a comment, a command invocation, and a blank line\n$tmpdir\n" 

Natürlich könnte man es so machen:

lappend output "set_var tmpdir $tmpdir_path" "this is a temporary directory"        0 
lappend output "set vdd $voltage"   "voltage gets its name from Alessandro Volta (1745 – 1827)" 1 
lappend output "set gnd 0.0"     "that's the ground voltage"         1 
lappend output "set temp $temperature"  "how hot or cold it is"          2 
lappend output "set topdir $topdir"   "that's the base of the working directory tree"    0 

set format "# %2\$s\n%1\$s%3\$s" 
# or: set format "%1\$s\t;# %2\$s%3\$s" 
# or: set format "%1\$s\n# %2\$s%3\$s" 

foreach {cmd com nls} $output { 
    puts $fileID [format $format $cmd $com [string repeat \n $nls]] 
} 

Auf diese Weise können Sie eine Ausgabe-Datenbank erhalten, die Sie verschiedene Arten anwenden können.

Dokumentation: foreach, format, lappend, puts, set, string

2

Verwenden Sie einfach puts "" eine Leerzeile hinzuzufügen. Alternativ verwenden Sie "\ n", um nach einem Text einen Zeilenumbruch hinzuzufügen. Einen Kommentar zu schreiben ist wie eine andere Zeile zu schreiben - nur dass die Zeile mit einem Hash beginnt.


% puts line1; puts ""; puts line2 
line1 

line2 
% 

% puts #line1; puts ""; puts line2 
#line1 

line2 
% 
Verwandte Themen