首页 > 解决方案 > 如何将 TCL 字典形成为字符串以将其作为字符串变量传递?

问题描述

我需要格式化一个可以解释为 TCL 字典的字符串。基本上,字符串是用 C++ 形成的,然后作为字符串变量值传递给 TCL 解释器,TCL 解释器应该能够将该字符串值视为字典。这可能吗,如果可以,应该如何格式化该字符串?

标签: dictionarytcl

解决方案


任何具有偶数个单词(由 tcl 定义)的字符串都是字典。

示例tclsh会话:

% set demo "a silly example string"
a silly example string
% dict get $demo a
silly
% dict get $demo example
string

如果您有多个单词的键或元素,则应将它们括在大括号中:

% set example "a {multi word value} {multi word key} test"
a {multi word value} {multi word key} test
% dict get $example a
multi word value
% dict get $example "multi word key"
test

推荐阅读