0

I'm trying to print a tcl variable , but its returning error:

set ay 126
% set val [format "%x" $ay]
7e
% puts "1::$val"
1::7e
% puts "111:$val::1"
can't read "val::1": no such variable
% puts "1::$val"
1::7e
% puts "111:$val"
111:7e
% puts "111:$val:1"
111:7e:1
%

Only when the variable is placed before a "::" the error is coming up.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
noobi
  • 97
  • 2
  • 6
  • Does this answer your question? [Double colon :: in Tcl](https://stackoverflow.com/questions/33441093/double-colon-in-tcl) - i.e. Tcl thinks you are asking for a variable in a specific namespace, as `::` is reserved for that meaning. – underscore_d Jun 29 '20 at 11:20
  • Actually I need it for manipulating ipv6 address. If :: is treated in a different way, does that mean in tcl we can't write ipv6 address in shortened form ? – noobi Jun 29 '20 at 11:25
  • 1
    It should be perfectly possible, just sounds like you probably need to 'escape' by using braces or backslashes; see e.g. https://wiki.tcl-lang.org/page/Tcl+Minimal+Escaping+Style and https://stackoverflow.com/questions/17188395/tcl-set-special-characters-in-a-string – underscore_d Jun 29 '20 at 11:29
  • 3
    You have enclose the variable name with braces . i.e. `puts "111:${val}::1"` – Dinesh Jun 29 '20 at 11:33

1 Answers1

1

If you want to substitute in a variable when the default boundary detection of variable names doesn't work (such as when there's a :: afterwards, because that's a namespace separator) put the variable name in {parentheses}:

puts "111:${val}::1"

Alternatively, build the string to print with format (this can be clearer in some cases):

puts [format "111:%s::1" $val]
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215