Re: dealing with special characters
- From: Alan Anderson <aranders@xxxxxxxxxxxxx>
- Date: Mon, 07 Jul 2008 22:04:26 -0400
In article
<50fd3a37-3c51-4828-92a1-81e0521df4fe@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Stuart <bigdakine@xxxxxxx> wrote:
Greetings Tclers...
Whats the best way to fix this?
The absolute first thing to do is to recognize the difference between a
(string representation of a well-formed) list and an arbitrary string.
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.
I then do this:
set MESS [lrange $MESS 1 end]
You MESSed it up here. The value you put in MESS is a string, but you
tried to use a list command on it. If it works at all, it can end up
escaping special characters, as you see:
puts "MESS $MESS"
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 happened because you asked Tcl to treat it as a list. In order to
preserve the apparently accidental "list"ness of the string, it did
special stuff.
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?
Are you trying to get everything after the space character? You can do
that correctly by frst turning the string into a proper list with the
[split] command.
set MESS [split $MESS]
And then you can keep all but the first word of the list by asking for
it:
set MESS [join [lrange $MESS 1 end] " "]
[split] breaks a string into a list (it defaults to using whitespace as
a delimiter). [join] concatenates the words of a list into a string.
The other thing you might want to consider is using braces instead of
double-quotes to surround your messy string. That way you don't need to
worry about escaping quotes brackets inside it.
.
- References:
- dealing with special characters
- From: Stuart
- dealing with special characters
- Prev by Date: dealing with special characters
- Next by Date: Re: Tcl source (was: newbie)
- Previous by thread: dealing with special characters
- Next by thread: Re: dealing with special characters
- Index(es):
Relevant Pages
|