Search code examples
tcl

Problbem with map function


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 ?


Solution

  • 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