Re: dealing with special characters
- From: Bryan Oakley <oakley@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 07 Jul 2008 23:55:39 -0500
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]
.
- Follow-Ups:
- Re: dealing with special characters
- From: Stuart
- Re: dealing with special characters
- References:
- dealing with special characters
- From: Stuart
- dealing with special characters
- Prev by Date: Re: Tcl source (was: newbie)
- Next by Date: Re: file rename -force failing silently
- Previous by thread: Re: dealing with special characters
- Next by thread: Re: dealing with special characters
- Index(es):
Relevant Pages
|