2

More often than not I get characters like "{\\ {" in my list which is the result of simple list which is concatenated or split by space .

Just looking for the reason behind these characters.

I know that the backspace followed by a special sign can be ignored from the equation but what does \\ mean ?

Zuckerberg
  • 205
  • 6
  • 15

2 Answers2

3

A long time ago, everything in Tcl was stored internally as a string, including lists. Whitespace was chosen to separate list elements so they could be distinguished from each other later...

set planets [list "mercury" "venus" "march"] ;# stored in planets as "mercury venus march"
puts [lindex $planets 2] ;# lindex treats it's first (string) argument as a list, and splits on whitespace to find the third element

...but then some way to escape/quote whitespace characters was needed to be able to store these in the actual list elements. { and } were used for this...

set programs [list "hello world" "primes"] ;# stored in programs as "{hello world} primes"

...and now you need to be able to escape { and }, etc.

In modern Tcl versions, lists are stored more efficiently internally, but from the script writers point of view it's still stored that way for backwards compability and maybe for other reasons. So when you use a list as a string, you get this string representation of the list, with spaces, braces and backslashes.

The simple solution to your question is to never treat a list as a string, i.e. don't send a list as an argument where the command expects a string. Then you'll never see any special escape sequences unless you actually put them in an element.

puts $mylist ;# bad unless used for debugging
puts [join $mylist] ;# good

And don't treat strings as lists either:

set mystring "word1 {hello word3"
puts [lindex $mystring 1] ;# will throw an error
puts [lindex [split $mystring] 1] ;# good
potrzebie
  • 1,768
  • 1
  • 12
  • 25
  • Thanks . Also ,Does output of a split command give out a string ? I do see lot of stray characters after a split . – Zuckerberg Jan 09 '13 at 10:17
  • @sudeepmathew No, `split` takes a string and returns a list, and `join` does the opposite. – potrzebie Jan 09 '13 at 11:01
  • +1 I wish I could upvote this answer more. To emphasize the point: don't treat a list as a string and vice versa. Use the conversion commands `split` and `join` – glenn jackman Jan 09 '13 at 14:09
  • I forgot to mention that the escape syntax that `list` uses is not random, it's done so that the resulting string, besides being used to extract the elements again, also can be used **as a script** where every element will become a word in the script. The result from `list $a $b $c` can be sent as a script to e.g. `eval` or `after`, and the command in `a` will be called with two arguments: the contents of `b` and `c`. `a`, `b` and `c` can contain any weird characters; they will be quoted in the list/script string. So that's one case (the only?) where you need to treat a list as a string. – potrzebie Jan 22 '13 at 03:38
2

\\ is an escaped backslash, which results in a simple backslash character that will not trigger special processing.

From the TCL manual:

If a backslash ("\") appears within a word then backslash substitution occurs. [...] [T]he 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 processing.

For example, to set variable x to the string literal abc[def\ghi you need to escape the [ and \ characters:

set x abc\[def\\ghi
Ergwun
  • 12,579
  • 7
  • 56
  • 83