StarCoder17
StarCoder17

Reputation: 175

writing multiple lines to a file in tcl, how to add blank line and comments

I am writing multiple lines to a file in tcl. Although i am successful to write out, but also wanted to have a blank lines after few lines and to add comment for each line.

Here goes my 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

Please suggest how to add blank lines and comment for each line.

Upvotes: 1

Views: 4648

Answers (2)

Peter Lewerin
Peter Lewerin

Reputation: 13272

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"

Of course, you could do it like this:

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]]
}

This way, you get an output database that you can apply different styles to.

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

Upvotes: 1

Sharad
Sharad

Reputation: 10612

Simply use puts "" to add a blank line. Alternatively use, puts "\n" to add a newline after some text. Writing a comment is like writing any other line - just that the line starts with a hash.


% puts line1; puts ""; puts line2
line1

line2
% 

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

line2
% 

Upvotes: 2

Related Questions