Morad
Morad

Reputation: 2779

escaping "\" in the end of a string in tcl

I want to create a string that ends with "\". For example:

set str {",$"23"^#@$\'"\}

This won't work because tcl thinks that I'm escaping the "}" here. So I tried to escape the "\"

set str {",$"23"^#@$\'"\\}

but now the value of str is ",$"23"^#@$\'"\\. I want the value of str to be with one "\" in the end: ",$"23"^#@$\'"\

How can I do that while creating the string inside {}

Upvotes: 0

Views: 418

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137787

You can't; this is one of the small number of cases that can't be quoted that way. Here's the proof from an interactive session:

% gets stdin s
",$"23"^#@$\'"\
15
% list $s
\",\$\"23\"^#@\$\\'\"\\

Other such cases are thing like where there's unbalanced braces, and so on. They really don't come up very often. That backslash form above generated by list is the alternative (and you can put it in double quotes if you wish).

Upvotes: 0

Mario Santini
Mario Santini

Reputation: 3003

The easiest way I could think is to use format:

puts [format {",$"23"^#@$\'"%s} \\]
",$"23"^#@$\'"\

I think you could even try with the %c and the ascii code of the \.

Upvotes: 1

Related Questions