Home > OS >  Is it possible to use various variables as a replacement entry in the string map of the tcl/tk?
Is it possible to use various variables as a replacement entry in the string map of the tcl/tk?

Time:11-27

I want to insert a variable with 'word' entry to be override within 'string map'. We usually do this:

# Input variable
set str "Is First Name"

# Result new strig 
puts [string map -nocase { {First Name} {Last Name} } $str]

what I need is to replace it through a second variable. For instance:

set replace "Last Name"

puts [string map -nocase { {First Name} $replace } $str]

I have the following output:

enter image description here

Before i asked, i did research, but I didn't find anything of the sort. I even tried to adapt other examples but to no avail.

CodePudding user response:

Remember Rule 6: no substitution is done inside braces. So you have to build the list some other way that does allow for variable substitution, like using list:

string map -nocase [list {First Name} $replace] $str

You could also use regsub instead of string map:

regsub -all -nocase {***=First Name} $str $replace

(The leading ***= in the regular expression means it's treated as an exact string and RE metacharacters lose their normal meaning)

  • Related