英文:
Problbem with map function
问题
我是TCL的初学者,我尝试使用map函数将变量重构到我的代码中。
未重构的源代码:
set data " version=\"1.03a\" ddgsdgfs"
set data [string map {version=\"1.03a\" version=\"2.1\"} $data]
没有问题,map函数起作用。
set arg_current_version "1.03a"
set arg_target_version "2.1"
set data " version=\"1.03a\" ddgsdgfs"
set version_current "version=\"$arg_current_version\""
set version_target "version=\"$arg_target_version\""
set data [string map {$version_current $version_target} $data]
不起作用... 有任何想法吗?
英文:
I'm beginner with TCL and I try to refactor my code with variables inside a map function.
The source code without refactoring :
set data " version=\"1.03a\" ddgsdgfs"
set data [string map {version=\"1.03a\" version=\"2.1\"} $data]
No problem, map works
set arg_current_version "1.03a"
set arg_target_version "2.1"
set data " version=\"1.03a\" ddgsdgfs"
set version_current "version=\"$arg_current_version\""
set version_target "version=\"$arg_target_version\""
set data [string map {$version_current $version_target} $data]
Not working ..... any idea ?
答案1
得分: 1
set data [string map {$version_current $version_target} $data]
在Tcl中,{花括号}
类似于shell中的单引号,它们是一种引用机制,阻止变量替换
您应该在那里使用list
命令:
set data [string map [list $version_current $version_target] $data]
请参阅Tcl语法的12条规则,第6条。
英文:
set data [string map {$version_current $version_target} $data]
In Tcl {braces}
are like the shell's single quotes -- they are a quoting mechanism that prevents variable substitution
You'll want to use the list
command there:
set data [string map [list $version_current $version_target] $data]
See the 12 rules of Tcl syntax, number 6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论