jay
jay

Reputation: 79

How to put out a complete string with {} in tcl

I have a line with special characters, I would like to do puts and newline everything in 1 LINE CODE. How do I do that without having to care about special characters or split them into multiple puts line?

Example:

puts $infile {I_want_$_"put_exactly_this_out$"_to_$infile\nI_also$#_"\n}

Here is what the output file should look like:

I want to eat_1_2_$

"I want to sleep"

I go to school\

All of the above, I would like to do a puts in 1 LINE. So in the script, I try to do the following, but doesnt seem to work.

puts $outfile1 "I want to eat_1_2_$\n"I want to sleep"\nI go to school\"

puts $outfile1 {I want to eat_1_2_$\n"I want to sleep"\nI go to school\}

Upvotes: 1

Views: 232

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

You could also do:

puts $out [string cat {I want to eat_1_2_$} \n {"I want to sleep"} \n {I go to school} \\]

or

puts $out [join {{I want to eat_1_2_$} {"I want to sleep"} "I go to school\\"} \n]

Upvotes: 0

Brad Lanam
Brad Lanam

Reputation: 5763

The quotes you want to output need to be escaped: Since you have characters you want interpreted (\n), using double quotes is easier in this case.

puts stdout "I want to eat_1_2_\$\n\"I want to sleep\"\nI go to school\\"

When you have complicated strings that mix special characters and escaped sequences and variables, it is generally easier to simply build them up piece by piece.

set x {I want to eat_1_2_$}
append x \n
append x {"I want to sleep"}
append x \n
append x {I go to school\\}
puts stdout $x

Upvotes: 1

Related Questions