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:
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)