Reputation: 95
I have a quotation problem in Tcl. I have a long list of signals from which in a for loop I assign each single one to my_sig
and then parse it.
foreach sig $list_of_sigs {
set my_sig $sig
}
One example of what I get back is:
set my_sig top.sub.sig[0]
How do I use my_sig
in commands to follow such that "[
" and "]
" are automatically escaped? For example, my commands accept:
my_command -filter signal_bit=="top.sub.sig\[0\]"
Thanks!
Upvotes: 0
Views: 208
Reputation: 137787
You don't normally have to do that, as Tcl doesn't reinterpret the contents of variables unless explicitly asked to. Something is expanding the value again and you probably ought to chase that down as it's actually insecure in a formal sense, and that tends to correlate with the code being slow too.
That said, it's pretty easy to add quoting with string map
. All it takes is a little care with the quoting of the backslashes.
foreach sig $list_of_sigs {
set my_sig [string map {[ {\[} ] {\]}} $sig]
# ... etc ...
}
Upvotes: 1
Reputation: 5763
You can use a regular expression to escape the special characters.
regsub -all {([\[\]])} $sig "\\\\\\1" sig
Upvotes: 1
Reputation: 247210
Try
set my_sig {top.sub.sig[0]}
my_command -filter signal_bit==$my_sig
Upvotes: 1