Post by ***@gmail.comHello,
set y "\" something \"";
puts $y ;# > " something"
puts "\u201D \u005C\u201D" ;# > ? \?
puts [string map {\" \\\"} $y] ; # > \" something \"
puts [string map {\u201D \u005C\u201D} $y] ;# > " something"
I'm guessing both puts (with string map) are the same
They are not.
Post by ***@gmail.com, however the last doesn't work as expected (by me).
Because it is different from the first.
Post by ***@gmail.comWhat i'm doing wrong ?
The string in $y does not contain a \u201D character. So there is
nothing for string map to replace.
Also, be careful of Tcl rule 6 (from man Tcl):
[6] Braces.
If the first character of a word is an open brace ("{")
and rule [5] does not apply, then the word is terminated
by the matching close brace ("}"). Braces nest within
the word: for each addi- tional open brace there must be
an additional close brace (how- ever, if an open brace or
close brace within the word is quoted with a backslash
then it is not counted in locating the matching close
brace). **No substitutions are performed on the
characters between the braces except for
backslash-newline substitutions described below, nor do
semi-colons, newlines, close brackets, or white space
receive any special interpretation.** The word will
consist of exactly the characters between the outer
braces, not including the braces themselves.
Your string maps appear to only work due to a quirk of being shimmered
from a string to a key value list when string map performs the mapping.
You can see this if you just set a variable to a braced string:
% set x {\u0022 \u005C\u201D}
\u0022 \u005C\u201D
% set x
\u0022 \u005C\u201D
The \u escapes were not interpreted, because of the braces. It is best
to use list for creating the mapping if there is anything in the map
that requires expansion:
string map [list \u201D \u005C\u201D] $y
Then the \u escapes are actual words that willl be guaranteed to be
handled by Tcl rule 9 (from man Tcl):
[9] Backslash substitution.
If a backslash ("\") appears within a word then
backslash sub- stitution occurs. In all cases but those
described below the backslash is dropped and the
following character is treated as an ordinary character
and included in the word. This allows characters such as
double quotes, close brackets, and dollar signs to be
included in words without triggering special pro-
cessing. ...