Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to handle weird strings with backslashes

31 views
Skip to first unread message

Stuart

unread,
May 17, 2013, 5:01:45 PM5/17/13
to

Greetings,

I need to process strings like
ddXaXD@@K@@LB\b@wHPJB\c@{HPJBHc@pH`NB|b@

This string has \ and { so I can't do:

set str {ddXaXD@@K@@LB\b@wHPJB\c@{HPJBHc@pH`NB|b@} cuz that
will complain about having an extra {

If I do

set str "ddXaXD@@K@@LB\b@wHPJB\c@{HPJBHc@pH`NB|b@"

The backslashes disappear. The backslashes in the above string do have
meaning, and actually what I would
like to do is change them to \\ so then I can process with the Tcl
command:

binary scan $str c* list which converts all characters, backslashes
included into their ASCII value

So if I manually insert extra slashes i.e.,

set str "ddXaXD@@K@@LB\\b@wHPJB\\c@{HPJBHc@pH`NB|b@"

then
binary scan $str c* list
puts $list gives
100 100 88 97 88 68 64 64 75 64 64 76 66 92 98 64 119 72 80 74 66 92
99 64 123 72 80 74 66 72 99 64 112 72 96 78 66 124 98 64

and the list length is the same as the string length which is what I
need. The ASCII value of slash is 92.

But how do I get there without manual intervention? Given that I have
slashes and brackets as part of the string?

Thanks in advance,

Stuart

DrS

unread,
May 17, 2013, 5:49:55 PM5/17/13
to
On 5/17/2013 5:01 PM, Stuart wrote:
>
> Greetings,
>
> I need to process strings like
> ddXaXD@@K@@LB\b@wHPJB\c@{HPJBHc@pH`NB|b@
>
> This string has \ and { so I can't do:
>
> set str {ddXaXD@@K@@LB\b@wHPJB\c@{HPJBHc@pH`NB|b@} cuz that
> will complain about having an extra {
>

This is only an issue when you treat it as a command like when entering
it in a shell or sourcing a file. If you read it as a data file, I
doubt it will be an issue. In any case, string map can help:

% set a {ddXaXD@@K@@LB\b@wHPJBc@\{HPJBHc@pH`NB|b@}
ddXaXD@@K@@LB\b@wHPJBc@\{HPJBHc@pH`NB|b@

% set b [string map {\\ \\\\ \{ \\\{ \} \\\}} $a]
ddXaXD@@K@@LB\\b@wHPJBc@\\\{HPJBHc@pH`NB|b@

% lindex $b 0
ddXaXD@@K@@LB\b@wHPJBc@\{HPJBHc@pH`NB|b@


Similarly, you can include [ and ] in the map.


DrS


Stuart

unread,
May 18, 2013, 7:09:23 PM5/18/13
to
Thanks a bunch.

Yours,

Stuart
0 new messages