Re: dealing with special characters



Stuart wrote:
Greetings Tclers...

Whats the best way to fix this?

I have an ugly string to parse

set MESS "15C3A33C 186005017\"B1B@
\y@]@@]G@]M@]R@]V@]W@]Y@]]@]]@]e@]k@]s@]}@^F@^R@^X@^^@^k@^s@_B@_L@_U@__@_g@_q@_}@_?
@`F@`G@`J@`Q@`Q@`X@``@`k@`t@aA@aM@aW@ae@am@a~@bD@bO@bT@bY@bb@bc@b]@b
\@bc@bo@b{@cF@cU@c\@cb@cl@cv@]R@^s@`]@b^@Lr:BATTLOAD 0 13.42 36+0NN
170W"

Note that the " just before the B needs to escaped for even the set
command to work.

Why is that surprising? How would Tcl know that the embedded quote is data rather than the closing "? If your data includes the same characters used for quoting the data you'll have to properly quote them.

I then do this:

set MESS [lrange $MESS 1 end]
puts "MESS $MESS"

Don't ever expect that to work.

First rule of thumb: never use list commands on strings unless you know for a fact the string is a well formed list (and most real world data fails that criteria). And once you treat it as a list, don't expect the string representation of the list to be the same as the original string. It may be, but it isn't guaranteed to be so.

And the output of puts is:
MESS 186005017\"B1B@y@\]@@\]G@\]M@\]R@\]V@\]W@\]Y@\]\]@\]\]@\]e@\]k@
\]s@\]\}@^F@^R@^X@^^@^k@^s@_B@_L@_U@__@_g@_q@_\}@_?
@`F@`G@`J@`Q@`Q@`X@``@`k@`t@aA@aM@aW@ae@am@a~@bD@bO@bT@bY@bb@bc@b
\]@b@bc@bo@b\{@cF@cU@c@cb@cl@cv@\]R@^s@`\]@b^@Lr:BATTLOAD 0 13.42
36+0NN 170W

Note that all of the []'s are escaped. I don't know why that happened.

It is because you asked Tcl to convert your string to a list and then convert the list back to a string. It is this second conversion that likely changed the way the data looks. This is normal and to be expected. If you treat lists as lists and strings as strings, it should never be a problem in practice.


My problem is, is that " [ ] are legitmate characeters in this
message. The additional \'s mess things up completely. I can't just
ignore them, cuz they are also legitimate characters.

Whats the best way to handle strings of this nature, so that the
string is preserved and I don't have to muck with
it? Are there any good solutions?

Use string commands on strings. Tcl will never add or remove quoting characters or otherwise change your data if you follow that rule.

for example: set MESS [string range $MESS 1 end]

.



Relevant Pages

  • Re: dealing with special characters
    ... that it wasn't part of the original string. ... puts "MESS $MESS" ... It is because you asked Tcl to convert your string to a list and then ... If you treat lists as lists and strings as strings, ...
    (comp.lang.tcl)
  • dealing with special characters
    ... I have an ugly string to parse ... set MESS ... Whats the best way to handle strings of this nature, ...
    (comp.lang.tcl)
  • Re: basics
    ... > int main ... // Why mess around with the file when you can just mess with the string? ... // char & get ...
    (alt.comp.lang.learn.c-cpp)
  • Re: dealing with special characters
    ... The absolute first thing to do is to recognize the difference between a ... list and an arbitrary string. ... set MESS ...
    (comp.lang.tcl)
  • Re: dealing with special characters
    ... If you have complicated strings in your source code and you do not ... set MESS {15C3A33C 186005017"B1B@ ... In contrast, if you use "" as string embracing characters, you may use ... set MESS ...
    (comp.lang.tcl)