问题出现在map函数中。

huangapple go评论65阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月1日 00:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定